forked from organicmaps/organicmaps
Lite routing banner.
This commit is contained in:
parent
c81843c06c
commit
53376be433
10 changed files with 349 additions and 18 deletions
BIN
android/res/drawable/close.png
Normal file
BIN
android/res/drawable/close.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 750 B |
21
android/res/layout/fragment_pro_routing.xml
Normal file
21
android/res/layout/fragment_pro_routing.xml
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<WebView
|
||||
android:id="@+id/wv__banner"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/btn__close"
|
||||
android:layout_width="22dp"
|
||||
android:layout_height="22dp"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_margin="@dimen/margin_small"
|
||||
android:layout_marginTop="@dimen/margin_small"
|
||||
android:background="@null"
|
||||
android:src="@drawable/close"/>
|
||||
|
||||
</RelativeLayout>
|
|
@ -101,6 +101,11 @@
|
|||
<item name="listPreferredItemPaddingRight">16dip</item>
|
||||
</style>
|
||||
|
||||
<style name="MWMTheme.Dialog.DialogFragment" parent="@android:style/Theme.Holo.Light" >
|
||||
<item name="android:windowBackground">@null</item>
|
||||
<item name="android:windowIsFloating">true</item>
|
||||
</style>
|
||||
|
||||
<style name="drawerSeparatorNoPadding">
|
||||
<item name="android:layout_height">1px</item>
|
||||
<item name="android:layout_width">match_parent</item>
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.graphics.Bitmap;
|
|||
import android.graphics.BitmapFactory;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.mapswithme.maps.BuildConfig;
|
||||
import com.mapswithme.maps.MWMApplication;
|
||||
import com.mapswithme.util.ConnectionState;
|
||||
import com.mapswithme.util.Constants;
|
||||
|
@ -31,6 +32,7 @@ import java.util.Locale;
|
|||
public class AdsManager
|
||||
{
|
||||
private static final String ROOT_MENU_ITEMS_KEY = "AppFeatureBottomMenuItems";
|
||||
private static final String ROOT_BANNERS_KEY = "AppFeatureBanners";
|
||||
private static final String MENU_ITEMS_KEY = "Items";
|
||||
private static final String DEFAULT_KEY = "*";
|
||||
private static final String ID_KEY = "Id";
|
||||
|
@ -41,42 +43,63 @@ public class AdsManager
|
|||
private static final String WEB_URL_KEY = "WebURLs";
|
||||
private static final String CACHE_FILE = "menu_ads.json";
|
||||
private static final String ID_APP_PACKAGE = "AppPackage";
|
||||
private static final String SHOW_LITE_KEY = "ShowInLite";
|
||||
private static final String SHOW_PRO_KEY = "ShowInPro";
|
||||
private static final String FG_TIME_KEY = "ForegroundTime";
|
||||
private static final String LAUNCH_NUM_KEY = "LaunchNumber";
|
||||
private static final String APP_VERSION_KEY = "AppVersion";
|
||||
private static final String BANNER_URL_KEY = "Url";
|
||||
|
||||
private static List<MenuAd> sMenuAds;
|
||||
private static List<Banner> sBanners;
|
||||
|
||||
public static List<MenuAd> getMenuAds()
|
||||
{
|
||||
return sMenuAds;
|
||||
}
|
||||
|
||||
public static void updateMenuAds()
|
||||
public static List<Banner> getBanners()
|
||||
{
|
||||
String menuAdsString;
|
||||
return sBanners;
|
||||
}
|
||||
|
||||
public static void updateFeatures()
|
||||
{
|
||||
String featuresString = null;
|
||||
if (ConnectionState.isConnected())
|
||||
{
|
||||
menuAdsString = getJsonAdsFromServer();
|
||||
cacheMenuAds(menuAdsString);
|
||||
featuresString = getJsonAdsFromServer();
|
||||
cacheFeatures(featuresString);
|
||||
}
|
||||
else
|
||||
menuAdsString = getCachedJsonString();
|
||||
|
||||
if (menuAdsString == null)
|
||||
if (featuresString == null)
|
||||
featuresString = getCachedJsonString();
|
||||
|
||||
if (featuresString == null)
|
||||
return;
|
||||
|
||||
final JSONObject menuAdsJson;
|
||||
JSONObject featuresJson = null;
|
||||
try
|
||||
{
|
||||
menuAdsJson = new JSONObject(menuAdsString);
|
||||
sMenuAds = parseMenuAds(menuAdsJson);
|
||||
featuresJson = new JSONObject(featuresString);
|
||||
sMenuAds = parseMenuAds(featuresJson);
|
||||
} catch (JSONException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
sBanners = parseBanners(featuresJson);
|
||||
} catch (JSONException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void cacheMenuAds(String menuAdsString)
|
||||
private static void cacheFeatures(String featuresString)
|
||||
{
|
||||
if (menuAdsString == null)
|
||||
if (featuresString == null)
|
||||
return;
|
||||
|
||||
final File cacheFile = new File(MWMApplication.get().getDataStoragePath(), CACHE_FILE);
|
||||
|
@ -84,7 +107,7 @@ public class AdsManager
|
|||
try
|
||||
{
|
||||
fileOutputStream = new FileOutputStream(cacheFile);
|
||||
fileOutputStream.write(menuAdsString.getBytes());
|
||||
fileOutputStream.write(featuresString.getBytes());
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e)
|
||||
{
|
||||
|
@ -122,6 +145,9 @@ public class AdsManager
|
|||
|
||||
private static List<MenuAd> parseMenuAds(JSONObject adsJson) throws JSONException
|
||||
{
|
||||
if (adsJson == null)
|
||||
return null;
|
||||
|
||||
final List<MenuAd> ads = new ArrayList<>();
|
||||
|
||||
final String countryCode = Locale.getDefault().getCountry();
|
||||
|
@ -153,6 +179,33 @@ public class AdsManager
|
|||
return ads;
|
||||
}
|
||||
|
||||
private static List<Banner> parseBanners(JSONObject rootJson) throws JSONException
|
||||
{
|
||||
if (rootJson == null)
|
||||
return null;
|
||||
final ArrayList<Banner> banners = new ArrayList<>();
|
||||
|
||||
final String countryCode = Locale.getDefault().getCountry();
|
||||
final JSONArray featuresJson = getJsonObjectByKeyOrDefault(rootJson.getJSONObject(ROOT_BANNERS_KEY), countryCode).
|
||||
getJSONArray(MENU_ITEMS_KEY);
|
||||
|
||||
for (int i = 0; i < featuresJson.length(); i++)
|
||||
{
|
||||
final JSONObject featureJson = featuresJson.getJSONObject(i);
|
||||
final String url = featureJson.optString(BANNER_URL_KEY);
|
||||
final boolean showLite = featureJson.optBoolean(SHOW_LITE_KEY, false);
|
||||
final boolean showPro = featureJson.optBoolean(SHOW_PRO_KEY, false);
|
||||
final int fgTime = featureJson.optInt(FG_TIME_KEY, 0);
|
||||
final int launchNum = featureJson.optInt(LAUNCH_NUM_KEY, 0);
|
||||
final int appVersion = featureJson.optInt(APP_VERSION_KEY, 0);
|
||||
final String id = featureJson.optString(ID_KEY);
|
||||
|
||||
banners.add(new Banner(url, showLite, showPro, launchNum, fgTime, appVersion, id));
|
||||
}
|
||||
|
||||
return banners;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and caches ad icon. If internet isnt connected tries to reuse cached icon.
|
||||
*
|
||||
|
@ -255,9 +308,9 @@ public class AdsManager
|
|||
HttpURLConnection connection = null;
|
||||
try
|
||||
{
|
||||
final URL url = new URL(Constants.Url.MENU_ADS_JSON);
|
||||
final URL url = new URL(Constants.Url.FEATURES_JSON);
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
final int timeout = 10000;
|
||||
final int timeout = 30000;
|
||||
connection.setReadTimeout(timeout);
|
||||
connection.setConnectTimeout(timeout);
|
||||
connection.setRequestMethod("GET");
|
||||
|
@ -287,4 +340,32 @@ public class AdsManager
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean shouldShowBanner(Banner banner)
|
||||
{
|
||||
final MWMApplication application = MWMApplication.get();
|
||||
return ((ConnectionState.isConnected()) &&
|
||||
((BuildConfig.IS_PRO && banner.getShowInPro()) || (!BuildConfig.IS_PRO && banner.getShowInLite())) &&
|
||||
(BuildConfig.VERSION_CODE >= banner.getAppVersion()) &&
|
||||
(application.nativeGetBoolean("ShouldShow" + banner.getId(), true)) &&
|
||||
(application.getForegroundTime() >= banner.getFgTime()) &&
|
||||
(application.getLaunchesNumber() >= banner.getLaunchNumber()));
|
||||
}
|
||||
|
||||
public static Banner getBannerToShow()
|
||||
{
|
||||
if (sBanners == null || sBanners.isEmpty())
|
||||
return null;
|
||||
|
||||
for (Banner banner : sBanners)
|
||||
{
|
||||
if (shouldShowBanner(banner))
|
||||
{
|
||||
MWMApplication.get().nativeSetBoolean("ShouldShow" + banner.getId(), false);
|
||||
return banner;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
112
android/src/com/mapswithme/maps/Ads/Banner.java
Normal file
112
android/src/com/mapswithme/maps/Ads/Banner.java
Normal file
|
@ -0,0 +1,112 @@
|
|||
package com.mapswithme.maps.Ads;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
public class Banner implements Parcelable
|
||||
{
|
||||
private String mUrl;
|
||||
private boolean mShowInLite;
|
||||
private boolean mShowInPro;
|
||||
private int mLaunchNumber;
|
||||
private int mForegroundTime;
|
||||
private int mAppVersion;
|
||||
private String mId;
|
||||
|
||||
public Banner(String url, boolean showInLite, boolean showInPro, int launchNum, int fgTime, int appVersion, String id)
|
||||
{
|
||||
mUrl = url;
|
||||
mShowInLite = showInLite;
|
||||
mShowInPro = showInPro;
|
||||
mLaunchNumber = launchNum;
|
||||
mForegroundTime = fgTime;
|
||||
mAppVersion = appVersion;
|
||||
mId = id;
|
||||
}
|
||||
|
||||
public Banner(Parcel source)
|
||||
{
|
||||
mUrl = source.readString();
|
||||
mShowInLite = source.readByte() == 1;
|
||||
mShowInPro = source.readByte() == 1;
|
||||
mLaunchNumber = source.readInt();
|
||||
mForegroundTime = source.readInt();
|
||||
mAppVersion = source.readInt();
|
||||
mId = source.readString();
|
||||
}
|
||||
|
||||
public int getFgTime()
|
||||
{
|
||||
return mForegroundTime;
|
||||
}
|
||||
|
||||
public boolean getShowInPro()
|
||||
{
|
||||
return mShowInPro;
|
||||
}
|
||||
|
||||
public String getUrl()
|
||||
{
|
||||
return mUrl;
|
||||
}
|
||||
|
||||
public int getLaunchNumber()
|
||||
{
|
||||
return mLaunchNumber;
|
||||
}
|
||||
|
||||
public boolean getShowInLite()
|
||||
{
|
||||
return mShowInLite;
|
||||
}
|
||||
|
||||
public int getAppVersion()
|
||||
{
|
||||
return mAppVersion;
|
||||
}
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return mId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Url " + mUrl + ", showLite " + mShowInLite + ", showPro " + mShowInPro + ", lanuchNum " +
|
||||
mLaunchNumber + ", fgTime " + mForegroundTime + ", appVersion " + mAppVersion + ", id " + mId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags)
|
||||
{
|
||||
dest.writeString(mUrl);
|
||||
dest.writeByte((byte) (mShowInLite ? 1 : 0));
|
||||
dest.writeByte((byte) (mShowInPro ? 1 : 0));
|
||||
dest.writeInt(mLaunchNumber);
|
||||
dest.writeInt(mForegroundTime);
|
||||
dest.writeInt(mAppVersion);
|
||||
dest.writeString(mId);
|
||||
}
|
||||
|
||||
public static final Creator<Banner> CREATOR = new Creator<Banner>()
|
||||
{
|
||||
@Override
|
||||
public Banner createFromParcel(Parcel source)
|
||||
{
|
||||
return new Banner(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Banner[] newArray(int size)
|
||||
{
|
||||
return new Banner[size];
|
||||
}
|
||||
};
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.mapswithme.maps.Ads;
|
||||
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.ImageButton;
|
||||
|
||||
import com.mapswithme.maps.BuildConfig;
|
||||
import com.mapswithme.maps.R;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Locale;
|
||||
|
||||
public class BannerDialogFragment extends DialogFragment implements View.OnClickListener
|
||||
{
|
||||
public static final String EXTRA_BANNER = "extra.banner";
|
||||
|
||||
private ImageButton mBtnClose;
|
||||
private WebView mWvBanner;
|
||||
private Banner mBanner;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
|
||||
{
|
||||
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
|
||||
mBanner = getArguments().getParcelable(EXTRA_BANNER);
|
||||
|
||||
final View view = inflater.inflate(R.layout.fragment_pro_routing, container, false);
|
||||
mBtnClose = (ImageButton) view.findViewById(R.id.btn__close);
|
||||
mBtnClose.setOnClickListener(this);
|
||||
mWvBanner = (WebView) view.findViewById(R.id.wv__banner);
|
||||
mWvBanner.getSettings().setJavaScriptEnabled(true);
|
||||
mWvBanner.setInitialScale(100);
|
||||
try
|
||||
{
|
||||
final View root = getActivity().findViewById(android.R.id.content);
|
||||
final StringBuilder builder = new StringBuilder(mBanner.getUrl())
|
||||
.append("?Width=").append(URLEncoder.encode(String.valueOf(root.getWidth()), "UTF-8"))
|
||||
.append("&Height=").append(URLEncoder.encode(String.valueOf(root.getHeight()), "UTF-8"))
|
||||
.append("&Lang=").append(URLEncoder.encode(Locale.getDefault().getLanguage(), "UTF-8"))
|
||||
.append("&Pro_URL=").append(URLEncoder.encode(BuildConfig.PRO_URL, "UTF-8"));
|
||||
mWvBanner.loadUrl(builder.toString());
|
||||
} catch (UnsupportedEncodingException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
switch (v.getId())
|
||||
{
|
||||
case R.id.btn__close:
|
||||
dismiss();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop()
|
||||
{
|
||||
super.onStop();
|
||||
|
||||
if (getActivity() != null)
|
||||
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
|
||||
}
|
||||
}
|
|
@ -16,6 +16,9 @@ import android.net.Uri;
|
|||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.SpannableStringBuilder;
|
||||
|
@ -40,6 +43,8 @@ import android.widget.Toast;
|
|||
import com.mapswithme.country.ActiveCountryTree;
|
||||
import com.mapswithme.country.DownloadActivity;
|
||||
import com.mapswithme.maps.Ads.AdsManager;
|
||||
import com.mapswithme.maps.Ads.Banner;
|
||||
import com.mapswithme.maps.Ads.BannerDialogFragment;
|
||||
import com.mapswithme.maps.Ads.MenuAd;
|
||||
import com.mapswithme.maps.Framework.OnBalloonListener;
|
||||
import com.mapswithme.maps.Framework.RoutingListener;
|
||||
|
@ -242,6 +247,7 @@ public class MWMActivity extends NvEventQueueActivity
|
|||
checkFacebookDialog();
|
||||
checkBuyProDialog();
|
||||
checkUserMarkActivation();
|
||||
checkShouldShowBanners();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -1094,6 +1100,23 @@ public class MWMActivity extends NvEventQueueActivity
|
|||
|
||||
}
|
||||
|
||||
private void checkShouldShowBanners()
|
||||
{
|
||||
final Banner banner = AdsManager.getBannerToShow();
|
||||
if (banner != null)
|
||||
{
|
||||
final DialogFragment fragment = new BannerDialogFragment();
|
||||
fragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.MWMTheme_Dialog_DialogFragment);
|
||||
final Bundle args = new Bundle();
|
||||
args.putParcelable(BannerDialogFragment.EXTRA_BANNER, banner);
|
||||
fragment.setArguments(args);
|
||||
final FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
FragmentTransaction transaction = fragmentManager.beginTransaction();
|
||||
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
||||
transaction.add(android.R.id.content, fragment).addToBackStack(null).commit();
|
||||
}
|
||||
}
|
||||
|
||||
private void tryResumeRouting()
|
||||
{
|
||||
if (Framework.nativeIsRoutingActive())
|
||||
|
|
|
@ -32,6 +32,7 @@ public class MWMApplication extends android.app.Application implements ActiveCou
|
|||
private final static String TAG = "MWMApplication";
|
||||
private static final CharSequence PRO_PACKAGE_POSTFIX = ".pro";
|
||||
private static final String FOREGROUND_TIME_SETTING = "AllForegroundTime";
|
||||
private static final String LAUNCH_NUMBER_SETTING = "LaunchNumber";
|
||||
|
||||
private static MWMApplication mSelf;
|
||||
|
||||
|
@ -151,6 +152,8 @@ public class MWMApplication extends android.app.Application implements ActiveCou
|
|||
BookmarkManager.getBookmarkManager();
|
||||
|
||||
WorkerService.startActionUpdateAds(this);
|
||||
|
||||
updateLaunchNumbers();
|
||||
}
|
||||
|
||||
public String getApkPath()
|
||||
|
@ -275,6 +278,16 @@ public class MWMApplication extends android.app.Application implements ActiveCou
|
|||
return !mwmDir.exists() || (System.currentTimeMillis() - mwmDir.lastModified() < TIME_DELTA);
|
||||
}
|
||||
|
||||
private void updateLaunchNumbers()
|
||||
{
|
||||
nativeSetInt(LAUNCH_NUMBER_SETTING, nativeGetInt(LAUNCH_NUMBER_SETTING, 0) + 1);
|
||||
}
|
||||
|
||||
public int getLaunchesNumber()
|
||||
{
|
||||
return nativeGetInt(LAUNCH_NUMBER_SETTING, 0);
|
||||
}
|
||||
|
||||
private void initMAT(Activity activity)
|
||||
{
|
||||
if (!Utils.hasAnyGoogleStoreInstalled())
|
||||
|
|
|
@ -205,7 +205,7 @@ public class WorkerService extends IntentService
|
|||
|
||||
private void updateMenuAds()
|
||||
{
|
||||
AdsManager.updateMenuAds();
|
||||
AdsManager.updateFeatures();
|
||||
final Intent broadcast = new Intent(ACTION_UPDATE_MENU_ADS);
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
||||
}
|
||||
|
|
|
@ -31,8 +31,8 @@ public class Constants
|
|||
public static final String MAIL_MAPSME_SUBSCRIBE = "subscribe@maps.me";
|
||||
|
||||
public static final String DATA_SCHEME_FILE = "file";
|
||||
public static final String MENU_ADS_JSON = "http://application.server/android/features.json";
|
||||
public static final String MENU_ADS_JSON_TEST = "http://application.server/android/test.json";
|
||||
public static final String FEATURES_JSON = "http://application.server/android/features.json";
|
||||
public static final String FEATURES_JSON_TEST = "http://application.server/android/test.json";
|
||||
|
||||
private Url() {}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue