From 1d0b093a6e384f5ade340c2b404b324bb6002c97 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Fri, 20 Mar 2015 18:51:38 -0700 Subject: Adding BaseActivity which can be overriden for custom behavior Change-Id: I6707aa99f5d33dceede226a6367e24482f5157f6 --- .../launcher3/LiveWallpaperListAdapter.java | 1 - .../android/launcher3/SavedWallpaperImages.java | 5 +- .../ThirdPartyWallpaperPickerListAdapter.java | 5 +- .../android/launcher3/WallpaperCropActivity.java | 26 +++---- .../android/launcher3/WallpaperPickerActivity.java | 81 ++++++++++++---------- .../com/android/launcher3/base/BaseActivity.java | 21 ++++++ 6 files changed, 80 insertions(+), 59 deletions(-) create mode 100644 WallpaperPicker/src/com/android/launcher3/base/BaseActivity.java (limited to 'WallpaperPicker') diff --git a/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java b/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java index a5e951ce4..b53fce119 100644 --- a/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java +++ b/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java @@ -30,7 +30,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; -import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.TextView; diff --git a/WallpaperPicker/src/com/android/launcher3/SavedWallpaperImages.java b/WallpaperPicker/src/com/android/launcher3/SavedWallpaperImages.java index 9f92bc105..64b0ac466 100644 --- a/WallpaperPicker/src/com/android/launcher3/SavedWallpaperImages.java +++ b/WallpaperPicker/src/com/android/launcher3/SavedWallpaperImages.java @@ -16,7 +16,6 @@ package com.android.launcher3; -import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -60,13 +59,13 @@ public class SavedWallpaperImages extends BaseAdapter implements ListAdapter { } } - public SavedWallpaperImages(Activity context) { + public SavedWallpaperImages(Context context) { // We used to store the saved images in the cache directory, but that meant they'd get // deleted sometimes-- move them to the data directory ImageDb.moveFromCacheDirectoryIfNecessary(context); mDb = new ImageDb(context); mContext = context; - mLayoutInflater = context.getLayoutInflater(); + mLayoutInflater = LayoutInflater.from(context); } public void loadThumbnailsAndImageIdList() { diff --git a/WallpaperPicker/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java b/WallpaperPicker/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java index 16bde3300..f46da53ec 100644 --- a/WallpaperPicker/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java +++ b/WallpaperPicker/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java @@ -28,7 +28,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; -import android.widget.FrameLayout; import android.widget.ListAdapter; import android.widget.TextView; @@ -38,8 +37,6 @@ import java.util.ArrayList; import java.util.List; public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements ListAdapter { - private static final String LOG_TAG = "LiveWallpaperListAdapter"; - private final LayoutInflater mInflater; private final PackageManager mPackageManager; private final int mIconSize; @@ -64,7 +61,7 @@ public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements } public ThirdPartyWallpaperPickerListAdapter(Context context) { - mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + mInflater = LayoutInflater.from(context); mPackageManager = context.getPackageManager(); mIconSize = context.getResources().getDimensionPixelSize(R.dimen.wallpaperItemIconSize); final PackageManager pm = mPackageManager; diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java index c238390e3..142a9cb10 100644 --- a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java +++ b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java @@ -43,6 +43,7 @@ import android.widget.Toast; import com.android.gallery3d.common.BitmapCropTask; import com.android.gallery3d.common.BitmapUtils; import com.android.gallery3d.common.Utils; +import com.android.launcher3.base.BaseActivity; import com.android.launcher3.util.Thunk; import com.android.launcher3.util.WallpaperUtils; import com.android.photos.BitmapRegionTileSource; @@ -54,7 +55,7 @@ import java.util.Collections; import java.util.Set; import java.util.WeakHashMap; -public class WallpaperCropActivity extends Activity implements Handler.Callback { +public class WallpaperCropActivity extends BaseActivity implements Handler.Callback { private static final String LOGTAG = "Launcher3.CropActivity"; protected static final String WALLPAPER_WIDTH_KEY = WallpaperUtils.WALLPAPER_WIDTH_KEY; @@ -86,7 +87,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback Collections.newSetFromMap(new WeakHashMap()); @Override - protected void onCreate(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLoaderThread = new HandlerThread("wallpaper_loader"); @@ -130,13 +131,12 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback // Load image in background final BitmapRegionTileSource.UriBitmapSource bitmapSource = - new BitmapRegionTileSource.UriBitmapSource(this, imageUri, 1024); + new BitmapRegionTileSource.UriBitmapSource(getContext(), imageUri, 1024); mSetWallpaperButton.setEnabled(false); Runnable onLoad = new Runnable() { public void run() { if (bitmapSource.getLoadingState() != BitmapSource.State.LOADED) { - Toast.makeText(WallpaperCropActivity.this, - getString(R.string.wallpaper_load_fail), + Toast.makeText(getContext(), R.string.wallpaper_load_fail, Toast.LENGTH_LONG).show(); finish(); } else { @@ -148,7 +148,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback } @Override - protected void onDestroy() { + public void onDestroy() { if (mCropView != null) { mCropView.destroy(); } @@ -203,7 +203,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback } } - req.result = new BitmapRegionTileSource(this, req.src, mTempStorageForDecoding); + req.result = new BitmapRegionTileSource(getContext(), req.src, mTempStorageForDecoding); runOnUiThread(new Runnable() { @Override @@ -291,9 +291,9 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback } protected void setWallpaper(Uri uri, final boolean finishActivityWhenDone) { - int rotation = BitmapUtils.getRotationFromExif(this, uri); + int rotation = BitmapUtils.getRotationFromExif(getContext(), uri); BitmapCropTask cropTask = new BitmapCropTask( - this, uri, null, rotation, 0, 0, true, false, null); + getContext(), uri, null, rotation, 0, 0, true, false, null); final Point bounds = cropTask.getImageBounds(); Runnable onEndCrop = new Runnable() { public void run() { @@ -330,7 +330,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback } } }; - BitmapCropTask cropTask = new BitmapCropTask(this, res, resId, + BitmapCropTask cropTask = new BitmapCropTask(getContext(), res, resId, crop, rotation, outSize.x, outSize.y, true, false, onEndCrop); cropTask.execute(); } @@ -422,7 +422,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback } } }; - BitmapCropTask cropTask = new BitmapCropTask(this, uri, + BitmapCropTask cropTask = new BitmapCropTask(getContext(), uri, cropRect, cropRotation, outWidth, outHeight, true, false, onEndCrop); if (onBitmapCroppedHandler != null) { cropTask.setOnBitmapCropped(onBitmapCroppedHandler); @@ -432,7 +432,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback protected void updateWallpaperDimensions(int width, int height) { String spKey = LauncherFiles.WALLPAPER_CROP_PREFERENCES_KEY; - SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS); + SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS); SharedPreferences.Editor editor = sp.edit(); if (width != 0 && height != 0) { editor.putInt(WALLPAPER_WIDTH_KEY, width); @@ -443,7 +443,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback } editor.commit(); WallpaperUtils.suggestWallpaperDimension(getResources(), - sp, getWindowManager(), WallpaperManager.getInstance(this), true); + sp, getWindowManager(), WallpaperManager.getInstance(getContext()), true); } static class LoadRequest { diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java index 152cac4cc..c49286a75 100644 --- a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java +++ b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java @@ -21,6 +21,7 @@ import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Activity; import android.app.WallpaperManager; +import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; @@ -159,8 +160,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { ViewGroup parent = (ViewGroup) mView.getParent(); if (parent != null) { parent.removeView(mView); - Toast.makeText(a, - a.getString(R.string.image_load_fail), + Toast.makeText(a.getContext(), R.string.image_load_fail, Toast.LENGTH_SHORT).show(); } } @@ -168,7 +168,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { }; } mBitmapSource = new BitmapRegionTileSource.UriBitmapSource( - a, mUri, BitmapRegionTileSource.MAX_PREVIEW_SIZE); + a.getContext(), mUri, BitmapRegionTileSource.MAX_PREVIEW_SIZE); a.setCropViewTileSource(mBitmapSource, true, false, null, onLoad); } @Override @@ -205,7 +205,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { @Override public void onClick(WallpaperPickerActivity a) { BitmapRegionTileSource.UriBitmapSource bitmapSource = - new BitmapRegionTileSource.UriBitmapSource(a, Uri.fromFile(mFile), 1024); + new BitmapRegionTileSource.UriBitmapSource(a.getContext(), + Uri.fromFile(mFile), 1024); a.setCropViewTileSource(bitmapSource, false, true, null, null); } @Override @@ -272,8 +273,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { @Override public void onClick(WallpaperPickerActivity a) { CropView c = a.getCropView(); - Drawable defaultWallpaper = WallpaperManager.getInstance(a).getBuiltInDrawable( - c.getWidth(), c.getHeight(), false, 0.5f, 0.5f); + Drawable defaultWallpaper = WallpaperManager.getInstance(a.getContext()) + .getBuiltInDrawable(c.getWidth(), c.getHeight(), false, 0.5f, 0.5f); if (defaultWallpaper == null) { Log.w(TAG, "Null default wallpaper encountered."); c.setTileSource(null, null); @@ -290,14 +291,15 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { return 1f; } }; - req.result = new DrawableTileSource(a, defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE); + req.result = new DrawableTileSource(a.getContext(), + defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE); a.onLoadRequestComplete(req, true); } @Override public void onSave(WallpaperPickerActivity a) { try { - WallpaperManager.getInstance(a).clear(); - a.setResult(RESULT_OK); + WallpaperManager.getInstance(a.getContext()).clear(); + a.setResult(Activity.RESULT_OK); } catch (IOException e) { Log.w("Setting wallpaper to default threw exception", e); } @@ -449,18 +451,18 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { // Populate the built-in wallpapers ArrayList wallpapers = findBundledWallpapers(); mWallpapersView = (LinearLayout) findViewById(R.id.wallpaper_list); - SimpleWallpapersAdapter ia = new SimpleWallpapersAdapter(this, wallpapers); + SimpleWallpapersAdapter ia = new SimpleWallpapersAdapter(getContext(), wallpapers); populateWallpapersFromAdapter(mWallpapersView, ia, false); // Populate the saved wallpapers - mSavedImages = new SavedWallpaperImages(this); + mSavedImages = new SavedWallpaperImages(getContext()); mSavedImages.loadThumbnailsAndImageIdList(); populateWallpapersFromAdapter(mWallpapersView, mSavedImages, true); // Populate the live wallpapers final LinearLayout liveWallpapersView = (LinearLayout) findViewById(R.id.live_wallpaper_list); - final LiveWallpaperListAdapter a = new LiveWallpaperListAdapter(this); + final LiveWallpaperListAdapter a = new LiveWallpaperListAdapter(getContext()); a.registerDataSetObserver(new DataSetObserver() { public void onChanged() { liveWallpapersView.removeAllViews(); @@ -474,7 +476,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { final LinearLayout thirdPartyWallpapersView = (LinearLayout) findViewById(R.id.third_party_wallpaper_list); final ThirdPartyWallpaperPickerListAdapter ta = - new ThirdPartyWallpaperPickerListAdapter(this); + new ThirdPartyWallpaperPickerListAdapter(getContext()); populateWallpapersFromAdapter(thirdPartyWallpapersView, ta, false); // Add a tile for the Gallery @@ -648,7 +650,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { // TODO: Remove this once the accessibility framework and // services have better support for selection state. v.announceForAccessibility( - getString(R.string.announce_selection, v.getContentDescription())); + getContext().getString(R.string.announce_selection, v.getContentDescription())); } @Thunk void initializeScrollForRtl() { @@ -666,7 +668,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } protected Bitmap getThumbnailOfLastPhoto() { - Cursor cursor = MediaStore.Images.Media.query(getContentResolver(), + Cursor cursor = MediaStore.Images.Media.query(getContext().getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN}, @@ -676,7 +678,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { if (cursor != null) { if (cursor.moveToNext()) { int id = cursor.getInt(0); - thumb = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), + thumb = MediaStore.Images.Thumbnails.getThumbnail(getContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null); } cursor.close(); @@ -684,7 +686,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { return thumb; } - protected void onStop() { + public void onStop() { super.onStop(); mWallpaperScrollContainer = (HorizontalScrollView) findViewById(R.id.wallpaper_scroll_container); if (mWallpaperScrollContainer.getAlpha() < 1f) { @@ -693,7 +695,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } } - protected void onSaveInstanceState(Bundle outState) { + public void onSaveInstanceState(Bundle outState) { outState.putParcelableArrayList(TEMP_WALLPAPER_TILES, mTempWallpaperTiles); outState.putInt(SELECTED_INDEX, mSelectedIndex); } @@ -818,7 +820,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { // Load the thumbnail final ImageView image = (ImageView) pickedImageThumbnail.findViewById(R.id.wallpaper_image); final Point defaultSize = getDefaultThumbnailSize(this.getResources()); - final Context context = this; + final Context context = getContext(); new AsyncTask() { protected Bitmap doInBackground(Void...args) { try { @@ -862,15 +864,16 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } } - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) { + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == IMAGE_PICK && resultCode == Activity.RESULT_OK) { if (data != null && data.getData() != null) { Uri uri = data.getData(); addTemporaryWallpaperTile(uri, false); } - } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY && resultCode == RESULT_OK) { + } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY + && resultCode == Activity.RESULT_OK) { // Something was set on the third-party activity. - setResult(RESULT_OK); + setResult(Activity.RESULT_OK); finish(); } } @@ -880,7 +883,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } private ArrayList findBundledWallpapers() { - final PackageManager pm = getPackageManager(); + final PackageManager pm = getContext().getPackageManager(); final ArrayList bundled = new ArrayList(24); Partner partner = Partner.get(pm); @@ -924,7 +927,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { Pair r = getWallpaperArrayResourceId(); if (r != null) { try { - Resources wallpaperRes = getPackageManager().getResourcesForApplication(r.first); + Resources wallpaperRes = getContext().getPackageManager() + .getResourcesForApplication(r.first); addWallpapers(bundled, wallpaperRes, r.first.packageName, r.second); } catch (PackageManager.NameNotFoundException e) { } @@ -947,7 +951,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { try { f.createNewFile(); FileOutputStream thumbFileStream = - openFileOutput(f.getName(), Context.MODE_PRIVATE); + getContext().openFileOutput(f.getName(), Context.MODE_PRIVATE); b.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream); thumbFileStream.close(); return true; @@ -959,17 +963,18 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } private File getDefaultThumbFile() { - return new File(getFilesDir(), Build.VERSION.SDK_INT + return new File(getContext().getFilesDir(), Build.VERSION.SDK_INT + "_" + LauncherFiles.DEFAULT_WALLPAPER_THUMBNAIL); } private boolean saveDefaultWallpaperThumb(Bitmap b) { // Delete old thumbnails. - new File(getFilesDir(), LauncherFiles.DEFAULT_WALLPAPER_THUMBNAIL_OLD).delete(); - new File(getFilesDir(), LauncherFiles.DEFAULT_WALLPAPER_THUMBNAIL).delete(); + new File(getContext().getFilesDir(), LauncherFiles.DEFAULT_WALLPAPER_THUMBNAIL_OLD).delete(); + new File(getContext().getFilesDir(), LauncherFiles.DEFAULT_WALLPAPER_THUMBNAIL).delete(); for (int i = Build.VERSION_CODES.JELLY_BEAN; i < Build.VERSION.SDK_INT; i++) { - new File(getFilesDir(), i + "_" + LauncherFiles.DEFAULT_WALLPAPER_THUMBNAIL).delete(); + new File(getContext().getFilesDir(), i + "_" + + LauncherFiles.DEFAULT_WALLPAPER_THUMBNAIL).delete(); } return writeImageToFileAsJpeg(getDefaultThumbFile(), b); } @@ -989,7 +994,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { Point defaultThumbSize = getDefaultThumbnailSize(res); int rotation = BitmapUtils.getRotationFromExif(res, resId); thumb = createThumbnail( - defaultThumbSize, this, null, null, sysRes, resId, rotation, false); + defaultThumbSize, getContext(), null, null, sysRes, resId, rotation, false); if (thumb != null) { defaultWallpaperExists = saveDefaultWallpaperThumb(thumb); } @@ -1011,7 +1016,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } else { Resources res = getResources(); Point defaultThumbSize = getDefaultThumbnailSize(res); - Drawable wallpaperDrawable = WallpaperManager.getInstance(this).getBuiltInDrawable( + Drawable wallpaperDrawable = WallpaperManager.getInstance(getContext()).getBuiltInDrawable( defaultThumbSize.x, defaultThumbSize.y, true, 0.5f, 0.5f); if (wallpaperDrawable != null) { thumb = Bitmap.createBitmap( @@ -1038,7 +1043,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { // package name should be. final String packageName = getResources().getResourcePackageName(R.array.wallpapers); try { - ApplicationInfo info = getPackageManager().getApplicationInfo(packageName, 0); + ApplicationInfo info = getContext().getPackageManager().getApplicationInfo(packageName, 0); return new Pair(info, R.array.wallpapers); } catch (PackageManager.NameNotFoundException e) { return null; @@ -1076,9 +1081,9 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { private static class SimpleWallpapersAdapter extends ArrayAdapter { private final LayoutInflater mLayoutInflater; - SimpleWallpapersAdapter(Activity activity, ArrayList wallpapers) { - super(activity, R.layout.wallpaper_picker_item, wallpapers); - mLayoutInflater = activity.getLayoutInflater(); + SimpleWallpapersAdapter(Context context, ArrayList wallpapers) { + super(context, R.layout.wallpaper_picker_item, wallpapers); + mLayoutInflater = LayoutInflater.from(context); } public View getView(int position, View convertView, ViewGroup parent) { @@ -1111,11 +1116,11 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } public void startActivityForResultSafely(Intent intent, int requestCode) { - Utilities.startActivityForResultSafely(this, intent, requestCode); + Utilities.startActivityForResultSafely(getActivity(), intent, requestCode); } @Override public boolean enableRotation() { - return Utilities.isRotationEnabled(this); + return Utilities.isRotationEnabled(getContext()); } } diff --git a/WallpaperPicker/src/com/android/launcher3/base/BaseActivity.java b/WallpaperPicker/src/com/android/launcher3/base/BaseActivity.java new file mode 100644 index 000000000..f8541188f --- /dev/null +++ b/WallpaperPicker/src/com/android/launcher3/base/BaseActivity.java @@ -0,0 +1,21 @@ +package com.android.launcher3.base; + +import android.app.Activity; +import android.content.Context; + +/** + * A wrapper over {@link Activity} which allows to override some methods. + * The base implementation can change from an Activity to a Fragment (or any other custom + * implementation), Callers should not assume that the base class extends Context, instead use + * either {@link #getContext} or {@link #getActivity} + */ +public class BaseActivity extends Activity { + + public Context getContext() { + return this; + } + + public Activity getActivity() { + return this; + } +} -- cgit v1.2.3