forked from organicmaps/organicmaps
Icons of ads in bottom menu.
This commit is contained in:
parent
d4708c7187
commit
be02bdb317
3 changed files with 87 additions and 3 deletions
|
@ -1,5 +1,8 @@
|
|||
package com.mapswithme.maps.Ads;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import com.mapswithme.maps.MWMApplication;
|
||||
import com.mapswithme.util.ConnectionState;
|
||||
import com.mapswithme.util.Constants;
|
||||
|
@ -15,6 +18,7 @@ import java.io.File;
|
|||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
@ -116,18 +120,84 @@ public class AdsManager
|
|||
final String icon = getStringByKeyOrDefault(menuItemJson.getJSONObject(ICON_KEY), density);
|
||||
final String webUrl = getStringByKeyOrDefault(menuItemJson.getJSONObject(WEB_URL_KEY), localeKey);
|
||||
final String title = getStringByKeyOrDefault(menuItemJson.getJSONObject(TITLE_KEY), localeKey);
|
||||
final String id = menuItemJson.getString(ID_KEY);
|
||||
final Bitmap bitmap = loadAdIcon(icon, id);
|
||||
|
||||
ads.add(new MenuAd(icon,
|
||||
title,
|
||||
menuItemJson.getString(COLOR_KEY),
|
||||
menuItemJson.getString(ID_KEY),
|
||||
id,
|
||||
menuItemJson.getString(APP_URL_KEY),
|
||||
webUrl));
|
||||
webUrl,
|
||||
bitmap));
|
||||
}
|
||||
|
||||
return ads;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and caches ad icon. If internet isnt connected tries to reuse cached icon.
|
||||
* @param urlString url of the icon
|
||||
* @param adId name of cachefile
|
||||
* @return
|
||||
*/
|
||||
private static Bitmap loadAdIcon(String urlString, String adId)
|
||||
{
|
||||
if (!ConnectionState.isConnected(MWMApplication.get()))
|
||||
return loadCachedBitmap(adId);
|
||||
|
||||
try
|
||||
{
|
||||
final URL url = new java.net.URL(urlString);
|
||||
final HttpURLConnection connection = (HttpURLConnection) url
|
||||
.openConnection();
|
||||
final int timeout = 5000;
|
||||
connection.setReadTimeout(timeout);
|
||||
connection.setConnectTimeout(timeout);
|
||||
connection.setDoInput(true);
|
||||
connection.connect();
|
||||
InputStream input = connection.getInputStream();
|
||||
final Bitmap bitmap = BitmapFactory.decodeStream(input);
|
||||
cacheBitmap(bitmap, adId);
|
||||
return bitmap;
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void cacheBitmap(Bitmap bitmap, String fileName)
|
||||
{
|
||||
final File cacheFile = new File(MWMApplication.get().getDataStoragePath(), fileName);
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(cacheFile))
|
||||
{
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Bitmap loadCachedBitmap(String fileName)
|
||||
{
|
||||
final File cacheFile = new File(MWMApplication.get().getDataStoragePath(), fileName);
|
||||
if (!cacheFile.exists())
|
||||
return null;
|
||||
|
||||
try (InputStream inputStream = new FileInputStream(cacheFile))
|
||||
{
|
||||
return BitmapFactory.decodeStream(inputStream);
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getStringByKeyOrDefault(JSONObject json, String key) throws JSONException
|
||||
{
|
||||
String res = json.optString(key);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.mapswithme.maps.Ads;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
|
@ -11,8 +12,9 @@ public class MenuAd implements Parcelable
|
|||
private String mId;
|
||||
private String mAppUrl;
|
||||
private String mWebUrl;
|
||||
private Bitmap mIcon;
|
||||
|
||||
public MenuAd(String iconUrl, String title, String hexColor, String id, String appUrl, String webUrl)
|
||||
public MenuAd(String iconUrl, String title, String hexColor, String id, String appUrl, String webUrl, Bitmap icon)
|
||||
{
|
||||
mIconUrl = iconUrl;
|
||||
mTitle = title;
|
||||
|
@ -20,6 +22,7 @@ public class MenuAd implements Parcelable
|
|||
mId = id;
|
||||
mAppUrl = appUrl;
|
||||
mWebUrl = webUrl;
|
||||
mIcon = icon;
|
||||
}
|
||||
|
||||
public MenuAd(Parcel source)
|
||||
|
@ -30,6 +33,7 @@ public class MenuAd implements Parcelable
|
|||
mId = source.readString();
|
||||
mAppUrl = source.readString();
|
||||
mWebUrl = source.readString();
|
||||
mIcon = source.readParcelable(Bitmap.class.getClassLoader());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,6 +72,11 @@ public class MenuAd implements Parcelable
|
|||
return mWebUrl;
|
||||
}
|
||||
|
||||
public Bitmap getIcon()
|
||||
{
|
||||
return mIcon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents()
|
||||
{
|
||||
|
@ -83,6 +92,7 @@ public class MenuAd implements Parcelable
|
|||
dest.writeString(mId);
|
||||
dest.writeString(mAppUrl);
|
||||
dest.writeString(mWebUrl);
|
||||
dest.writeParcelable(mIcon, 0);
|
||||
}
|
||||
|
||||
public static final Creator<MenuAd> CREATOR = new Creator<MenuAd>()
|
||||
|
|
|
@ -24,6 +24,7 @@ import android.view.View;
|
|||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
@ -769,6 +770,9 @@ public class MWMActivity extends NvEventQueueActivity
|
|||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
final ImageView imageView = (ImageView) view.findViewById(R.id.iv__bottom_icon);
|
||||
imageView.setImageBitmap(ad.getIcon());
|
||||
|
||||
view.setOnClickListener(new OnClickListener()
|
||||
{
|
||||
@Override
|
||||
|
|
Loading…
Add table
Reference in a new issue