Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MytargetHelper.java « statistics « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02e8334080c7dcb25e879f537ca3011ee4a94475 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.mapswithme.util.statistics;

import android.app.Activity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import com.mapswithme.maps.PrivateVariables;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.concurrency.ThreadPool;
import com.mapswithme.util.concurrency.UiThread;
import com.my.target.nativeads.NativeAppwallAd;
import com.my.target.nativeads.banners.NativeAppwallBanner;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import static com.mapswithme.maps.MwmApplication.prefs;

public final class MytargetHelper
{
  // for caching of myTarget setting achieved from server
  private static final String PREF_CHECK = "MyTargetCheck";
  private static final String PREF_CHECK_MILLIS = "MyTargetCheckTimestamp";
  private static final String CHECK_URL = PrivateVariables.myTargetCheckUrl();
  private static final long CHECK_INTERVAL_MILLIS = PrivateVariables.myTargetCheckInterval() * 1000;

  private static final int TIMEOUT = 1000;

  @Nullable
  private NativeAppwallAd mShowcase;
  private boolean mCancelled;

  public interface Listener<T>
  {
    void onNoAds();
    void onDataReady(@Nullable T data);
  }

  public MytargetHelper(final @NonNull Listener<Void> listener)
  {
    if (!ConnectionState.isConnected())
    {
      listener.onNoAds();
      return;
    }

    ThreadPool.getWorker().execute(new Runnable()
    {
      @Override
      public void run()
      {
        final boolean showShowcase = getShowcaseSetting();

        if (mCancelled)
          return;

        UiThread.run(new Runnable()
        {
          @Override
          public void run()
          {
            if (mCancelled)
              return;

            if (showShowcase)
              listener.onDataReady(null);
            else
              listener.onNoAds();
          }
        });
      }
    });
  }

  public void cancel()
  {
    mCancelled = true;
  }

  @WorkerThread
  private static boolean getShowcaseSetting()
  {
    final long lastCheckMillis = prefs().getLong(PREF_CHECK_MILLIS, 0);
    final long currentMillis = System.currentTimeMillis();
    if (currentMillis - lastCheckMillis < CHECK_INTERVAL_MILLIS)
      return isShowcaseSwitchedOnServer();

    HttpURLConnection connection = null;
    try
    {
      final URL url = new URL(CHECK_URL);
      connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("HEAD");
      // bugfix for HEAD requests on pre-JB devices https://code.google.com/p/android/issues/detail?id=24672
      connection.setRequestProperty("Accept-Encoding", "");
      connection.setConnectTimeout(TIMEOUT);
      connection.setReadTimeout(TIMEOUT);
      connection.connect();

      final boolean showShowcase = connection.getResponseCode() == HttpURLConnection.HTTP_OK;
      setShowcaseSwitchedOnServer(showShowcase);

      return showShowcase;
    } catch (MalformedURLException ignored)
    {
    } catch (IOException e)
    {
      e.printStackTrace();
    } finally
    {
      if (connection != null)
        connection.disconnect();
    }

    return false;
  }

  public void loadShowcase(final @NonNull Listener<List<NativeAppwallBanner>> listener, Activity activity)
  {
    if (mShowcase == null)
      mShowcase = loadAds(listener, activity);
  }

  public void handleBannersShow(@NonNull List<NativeAppwallBanner> banners)
  {
    if (mShowcase != null)
      mShowcase.handleBannersShow(banners);
  }

  private NativeAppwallAd loadAds(final @NonNull Listener<List<NativeAppwallBanner>> listener, Activity activity)
  {
    NativeAppwallAd res = new NativeAppwallAd(PrivateVariables.myTargetSlot(), activity);
    res.setListener(new NativeAppwallAd.AppwallAdListener()
    {
      @Override
      public void onLoad(NativeAppwallAd ad)
      {
        if (mCancelled)
          return;

        if (ad.getBanners().isEmpty())
          listener.onNoAds();
        else
          listener.onDataReady(ad.getBanners());
      }

      @Override
      public void onNoAd(String s, NativeAppwallAd nativeAppwallAd)
      {
        listener.onNoAds();
      }

      @Override
      public void onClick(NativeAppwallBanner nativeAppwallBanner, NativeAppwallAd nativeAppwallAd) {}

      @Override
      public void onDisplay(@NonNull NativeAppwallAd nativeAppwallAd)
      {
        /* Do nothing */
      }

      @Override
      public void onDismiss(@NonNull NativeAppwallAd nativeAppwallAd)
      {
        /* Do nothing */
      }
    });

    res.load();
    return res;
  }

  public void onBannerClick(@NonNull NativeAppwallBanner banner)
  {
    if (mShowcase != null)
      mShowcase.handleBannerClick(banner);
  }

  public static boolean isShowcaseSwitchedOnServer()
  {
    return prefs().getBoolean(PREF_CHECK, true);
  }

  private static void setShowcaseSwitchedOnServer(boolean switchedOn)
  {
    prefs().edit()
           .putLong(PREF_CHECK_MILLIS, System.currentTimeMillis())
           .putBoolean(PREF_CHECK, switchedOn)
           .apply();
  }
}