Permalink
Browse files

Work around 1.6 + AdMob incompatibilities. Fixes random crashes on 1.6.

  • Loading branch information...
1 parent b9bf91c commit 71526d2debe31b231f47a38dcd3408dca44f3281 @sole sole committed Apr 18, 2012
View
2 res/values/ids.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="text" type="id"/>
-
+ <item name="ad" type="id" />
</resources>
View
28 src/net/robotmedia/acv/logic/AdMobProxy.java
@@ -15,6 +15,7 @@
private static Method adRequestAddTestDeviceMethod = null;
private static Constructor adViewConstructor = null;
private static Method adViewLoadMethod = null;
+ private static Method adViewDestroyMethod = null;
private static void init() {
if (adViewConstructor == null) {
@@ -26,6 +27,7 @@ private static void init() {
adRequestAddTestDeviceMethod = adRequestClass.getMethod("addTestDevice", String.class);
adViewConstructor = adViewClass.getConstructor(Activity.class, adSizeClass, String.class);
adViewLoadMethod = adViewClass.getMethod("loadAd", adRequestClass);
+ adViewDestroyMethod = adViewClass.getMethod("destroy");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
@@ -77,6 +79,32 @@ public static View getAd(Activity activity, int size, String publisherId, String
return null;
}
+ /**
+ * Apparently there's a serious bug with WebViews + Activity.onDestroy in Android 1.5 and 1.6.
+ * As AdMob uses WebViews, it forces the application to crash.
+ * The work-around is to destroy AdViews in activity.onDestroy.
+ * This method removes the ad with id = viewId in the activity view hierarchy.
+ *
+ * @param activity
+ * @param viewId
+ */
+ public static void destroyAds(Activity activity, int viewId) {
+ if(adViewDestroyMethod != null) {
+ View v = activity.findViewById(viewId);
+ if(v != null) {
+ try {
+ adViewDestroyMethod.invoke(v);
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
private static final String BANNER = "BANNER";
private static final String IAB_MRECT = "IAB_MRECT";
private static final String IAB_BANNER = "IAB_BANNER";
View
11 src/net/robotmedia/acv/logic/AdsManager.java
@@ -1,5 +1,6 @@
package net.robotmedia.acv.logic;
+import net.androidcomics.acv.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
@@ -23,7 +24,9 @@
public static View getAd(Activity activity, int size) {
init(activity);
if (usesAds) {
- return AdMobProxy.getAd(activity, size, publisherId, testDeviceId);
+ View ad = AdMobProxy.getAd(activity, size, publisherId, testDeviceId);
+ ad.setId(R.id.ad);
+ return ad;
}
return null;
}
@@ -43,7 +46,11 @@ public static View getAd(Activity activity) {
return null;
}
-
+ public static void destroyAds(Activity activity) {
+ if(usesAds) {
+ AdMobProxy.destroyAds(activity, R.id.ad);
+ }
+ }
protected static void init(Context context) {
if (usesAds) {
View
12 src/net/robotmedia/acv/ui/ComicViewerActivity.java
@@ -84,7 +84,7 @@ protected void onPostExecute (Comic result) {
mScreen.goToScreen(initialIndex);
if(isHoneyComb()) {
- invalidateOptionsMenu();
+ new MenuHelper().invalidateOptionsMenu();
}
hideActionBar();
@@ -167,7 +167,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
adjustBrightness();
adjustLowMemoryMode();
-
+
if (isComicLoaded()) mScreen.goToScreen(mScreen.getIndex());
} else if (requestCode == Constants.SUBSCRIBE_CODE) {
switch (resultCode) {
@@ -932,12 +932,12 @@ private void loadComic(final String comicPath) {
private void loadComic(final String comicPath, final int initialIndex) {
final File file = new File(comicPath);
if (file.exists()) {
+ hideAds();
+
mComicPath = comicPath;
loadComicTask = new LoadComicTask();
loadComicTask.initialIndex = initialIndex;
loadComicTask.execute(comicPath);
-
- hideAds();
}
}
@@ -1143,7 +1143,8 @@ protected void removePreviousComic(boolean emptyTemp) {
if (emptyTemp) {
File tempDirectory = new File(Environment.getExternalStorageDirectory(), Constants.TEMP_PATH);
- FileUtils.deleteDirectory(tempDirectory);
+ if(tempDirectory.exists())
+ FileUtils.deleteDirectory(tempDirectory);
}
if (comic != null) {
comic.destroy();
@@ -1176,6 +1177,7 @@ private void showAds() {
}
private void hideAds() {
+ AdsManager.destroyAds(this);
mAdsContainer.removeAllViews();
}
View
70 src/net/robotmedia/acv/ui/ExtendedActivity.java
@@ -17,6 +17,7 @@
import java.util.HashSet;
+import net.robotmedia.acv.logic.AdsManager;
import net.robotmedia.acv.logic.TrackingManager;
import android.app.ActionBar;
import android.app.Activity;
@@ -40,7 +41,9 @@ public void setCanBeKilledByChild(boolean value) {
protected Runnable mHideActionBarRunnable = new Runnable() {
@Override
public void run() {
- hideActionBar();
+ if(isHoneyComb()) {
+ new ActionBarHelper().hide();
+ }
}
};
protected Handler mHandler;
@@ -60,12 +63,17 @@ public void onStop() {
@Override
protected void onDestroy() {
+
+ AdsManager.destroyAds(this);
+
super.onDestroy();
+
for (AsyncTask<?, ?, ?> task : mTasks) {
if (task.getStatus() != AsyncTask.Status.FINISHED) {
task.cancel(true);
}
}
+
}
@Override
@@ -89,28 +97,26 @@ protected void onPostCreate(Bundle savedInstanceState) {
mHandler = new Handler(looper);
if (isHoneyComb()) {
- mActionBar = getActionBar();
- if (mActionBar != null) {
- mActionBar.setDisplayShowHomeEnabled(false);
- mActionBar.setDisplayShowTitleEnabled(false);
- mActionBar.setDisplayHomeAsUpEnabled(false);
- mActionBar.setDisplayShowCustomEnabled(true);
- mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- }
-
+ setupActionBar();
hideActionBar();
}
}
+
+ protected void setupActionBar() {
+ if (isHoneyComb()) {
+ new ActionBarHelper().setup();
+ }
+ }
protected void showActionBar() {
if (isHoneyComb()) {
- mActionBar.show();
+ new ActionBarHelper().show();
}
}
protected void hideActionBar() {
if (isHoneyComb()) {
- mActionBar.hide();
+ new ActionBarHelper().hide();
}
}
@@ -120,15 +126,15 @@ protected void hideActionBarDelayed() {
mHandler.postDelayed(mHideActionBarRunnable, 7000);
}
}
-
+
// Return true if the action bar ends up being shown
protected boolean toggleControls() {
if(isHoneyComb()) {
mHandler.removeCallbacks(mHideActionBarRunnable);
- if(mActionBar.isShowing()) {
- mActionBar.hide();
+ if(new ActionBarHelper().isShowing()) {
+ new ActionBarHelper().hide();
} else {
- mActionBar.show();
+ new ActionBarHelper().show();
return true;
}
}
@@ -143,4 +149,36 @@ protected boolean isIcecream() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
+ protected class ActionBarHelper {
+
+ public void setup() {
+ ActionBar actionBar = getActionBar();
+
+ if (actionBar != null) {
+ actionBar.setDisplayShowHomeEnabled(false);
+ actionBar.setDisplayShowTitleEnabled(false);
+ actionBar.setDisplayHomeAsUpEnabled(false);
+ actionBar.setDisplayShowCustomEnabled(true);
+ actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
+ }
+ }
+
+ public void hide() {
+ getActionBar().hide();
+ }
+
+ public void show() {
+ getActionBar().show();
+ }
+
+ public boolean isShowing() {
+ return getActionBar().isShowing();
+ }
+ }
+
+ protected class MenuHelper {
+ public void invalidateOptionsMenu() {
+ ExtendedActivity.this.invalidateOptionsMenu();
+ }
+ }
}
View
1 src/net/robotmedia/acv/ui/settings/SettingsActivityPostHC.java
@@ -21,7 +21,6 @@
public class SettingsActivityPostHC extends ExtendedPreferenceActivity {
-
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
View
7 src/net/robotmedia/acv/ui/settings/SettingsActivityPreHC.java
@@ -41,4 +41,11 @@ protected void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.preferences);
}
+ @Override
+ protected void onDestroy() {
+
+ AdsManager.destroyAds(this);
+
+ super.onDestroy();
+ }
}

0 comments on commit 71526d2

Please sign in to comment.