From 33679232291538f570425ae93b9175b69c420460 Mon Sep 17 00:00:00 2001 From: Clark Scheff Date: Thu, 28 Aug 2014 10:59:39 -0700 Subject: Use a ContentObserver to listen for changes of installed themes This solves two issues. If a theme is installed while in the theme chooser, the themes will be re-populated with the newly installed theme and if a theme is uninstalled the themes will also be re-populated. Change-Id: I960d6fbb79b18258dca79f00fc380878fe334878 --- src/com/cyngn/theme/chooser/ChooserActivity.java | 38 +++++++++++++++++++++--- src/com/cyngn/theme/chooser/ThemeFragment.java | 19 +----------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/com/cyngn/theme/chooser/ChooserActivity.java b/src/com/cyngn/theme/chooser/ChooserActivity.java index b96f59f..76f1e5b 100644 --- a/src/com/cyngn/theme/chooser/ChooserActivity.java +++ b/src/com/cyngn/theme/chooser/ChooserActivity.java @@ -14,6 +14,7 @@ import android.content.IntentFilter; import android.content.res.Resources; import android.content.res.ThemeConfig; import android.content.res.ThemeManager; +import android.database.ContentObserver; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.ColorMatrix; @@ -66,7 +67,6 @@ public class ChooserActivity extends FragmentActivity private static final String TAG = ChooserActivity.class.getSimpleName(); public static final String DEFAULT = ThemeConfig.HOLO_DEFAULT; - public static final int REQUEST_UNINSTALL = 1; // Request code public static final String EXTRA_PKGNAME = "pkgName"; private static final int OFFSCREEN_PAGE_LIMIT = 3; @@ -108,6 +108,8 @@ public class ChooserActivity extends FragmentActivity ImageView mCustomBackground; + private ThemesObserver mThemesObserver; + // Current system theme configuration as component -> pkgName private Map mCurrentTheme = new HashMap(); @@ -431,6 +433,11 @@ public class ChooserActivity extends FragmentActivity // clear out the newly installed themes count PreferenceUtils.setNewlyInstalledThemeCount(this, 0); + + // register content observer for changes in installed themes + mThemesObserver = new ThemesObserver(new Handler()); + getContentResolver().registerContentObserver(ThemesColumns.CONTENT_URI, true, + mThemesObserver); } @Override @@ -480,6 +487,12 @@ public class ChooserActivity extends FragmentActivity if (f != null) { mSelectedTheme = f.getThemePackageName(); } + + // unregister our installed themes content observer + if (mThemesObserver != null) { + getContentResolver().unregisterContentObserver(mThemesObserver); + mThemesObserver = null; + } } @Override @@ -674,6 +687,26 @@ public class ChooserActivity extends FragmentActivity } + class ThemesObserver extends ContentObserver { + + /** + * Creates a content observer. + * + * @param handler The handler to run {@link #onChange} on, or null if none. + */ + public ThemesObserver(Handler handler) { + super(handler); + } + + @Override + public void onChange(boolean selfChange) { + mAdapter = new ThemesAdapter(ChooserActivity.this); + mPager.setAdapter(mAdapter); + getSupportLoaderManager().restartLoader(LOADER_ID_INSTALLED_THEMES, null, + ChooserActivity.this); + } + } + public class ThemesAdapter extends FragmentStatePagerAdapter { private Cursor mCursor; private Context mContext; @@ -704,9 +737,6 @@ public class ChooserActivity extends FragmentActivity @Override public int getItemPosition(Object object) { ThemeFragment fragment = (ThemeFragment) object; - if (fragment.isUninstalled()) { - return POSITION_NONE; - } return super.getItemPosition(object); } diff --git a/src/com/cyngn/theme/chooser/ThemeFragment.java b/src/com/cyngn/theme/chooser/ThemeFragment.java index ee7c896..3aafaee 100644 --- a/src/com/cyngn/theme/chooser/ThemeFragment.java +++ b/src/com/cyngn/theme/chooser/ThemeFragment.java @@ -227,7 +227,6 @@ public class ThemeFragment extends Fragment implements LoaderManager.LoaderCallb protected Handler mHandler; - protected boolean mIsUninstalled; protected int mActiveCardId = -1; protected ComponentSelector mSelector; // Supported components for the theme this fragment represents @@ -2008,23 +2007,7 @@ public class ThemeFragment extends Fragment implements LoaderManager.LoaderCallb Uri packageURI = Uri.parse("package:" + mPkgName); Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI); uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, true); - startActivityForResult(uninstallIntent, - ChooserActivity.REQUEST_UNINSTALL); - } - - public void onActivityResult (int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_UNINSTALL) { - try { - ApplicationInfo ainfo = getActivity() - .getPackageManager().getApplicationInfo(mPkgName, 0); - } catch (PackageManager.NameNotFoundException e) { - mIsUninstalled = true; - } - } - } - - public boolean isUninstalled() { - return mIsUninstalled; + startActivity(uninstallIntent); } public void setCurrentTheme(Map currentTheme) { -- cgit v1.1