Merge "Add BackgroundManager release." into lmp-mr1-ub-dev

This commit is contained in:
Craig Stout
2015-04-02 20:04:10 +00:00
committed by Android (Google) Code Review
6 changed files with 61 additions and 18 deletions

View File

@@ -100,6 +100,7 @@ public class BackgroundHelper {
private Bitmap loadBitmap(Activity activity, Object imageToken) { private Bitmap loadBitmap(Activity activity, Object imageToken) {
if (imageToken instanceof Integer) { if (imageToken instanceof Integer) {
final int resourceId = (Integer) imageToken; final int resourceId = (Integer) imageToken;
if (DEBUG) Log.v(TAG, "load resourceId " + resourceId);
Drawable drawable = ContextCompat.getDrawable(activity, resourceId); Drawable drawable = ContextCompat.getDrawable(activity, resourceId);
if (drawable instanceof BitmapDrawable) { if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap(); return ((BitmapDrawable) drawable).getBitmap();
@@ -110,7 +111,7 @@ public class BackgroundHelper {
private void applyBackground(Activity activity, Bitmap bitmap) { private void applyBackground(Activity activity, Bitmap bitmap) {
BackgroundManager backgroundManager = BackgroundManager.getInstance(activity); BackgroundManager backgroundManager = BackgroundManager.getInstance(activity);
if (bitmap == null || backgroundManager == null || !backgroundManager.isAttached()) { if (backgroundManager == null || !backgroundManager.isAttached()) {
return; return;
} }
backgroundManager.setBitmap(bitmap); backgroundManager.setBitmap(bitmap);
@@ -120,22 +121,34 @@ public class BackgroundHelper {
private LoadBackgroundRunnable mRunnable; private LoadBackgroundRunnable mRunnable;
private LoadBitmapTask mTask; private LoadBitmapTask mTask;
public void attach(Activity activity) { // Allocate a dedicated handler because there may be no view available
if (!ENABLED) { // when setBackground is invoked.
return; private Handler mHandler = new Handler();
}
BackgroundManager.getInstance(activity).attach(activity.getWindow());
}
public void setBackground(Activity activity, Object imageToken) { public void setBackground(Activity activity, Object imageToken) {
if (!ENABLED) { if (!ENABLED) {
return; return;
} }
Handler handler = activity.getWindow().getDecorView().getHandler();
if (mRunnable != null) { if (mRunnable != null) {
handler.removeCallbacks(mRunnable); mHandler.removeCallbacks(mRunnable);
} }
mRunnable = new LoadBackgroundRunnable(activity, imageToken); mRunnable = new LoadBackgroundRunnable(activity, imageToken);
handler.postDelayed(mRunnable, SET_BACKGROUND_DELAY_MS); mHandler.postDelayed(mRunnable, SET_BACKGROUND_DELAY_MS);
}
static public void attach(Activity activity) {
if (!ENABLED) {
return;
}
if (DEBUG) Log.v(TAG, "attach to activity " + activity);
BackgroundManager.getInstance(activity).attach(activity.getWindow());
}
static public void release(Activity activity) {
if (!ENABLED) {
return;
}
if (DEBUG) Log.v(TAG, "release from activity " + activity);
BackgroundManager.getInstance(activity).release();
} }
} }

View File

@@ -23,4 +23,16 @@ public class BrowseActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.browse); setContentView(R.layout.browse);
} }
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
BackgroundHelper.attach(this);
}
@Override
public void onStop() {
BackgroundHelper.release(this);
super.onStop();
}
} }

View File

@@ -67,7 +67,10 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm
RowPresenter.ViewHolder rowViewHolder, Row row) { RowPresenter.ViewHolder rowViewHolder, Row row) {
Log.i(TAG, "onItemSelected: " + item + " row " + row); Log.i(TAG, "onItemSelected: " + item + " row " + row);
if (item instanceof PhotoItem) { if (isShowingHeaders()) {
mBackgroundHelper.setBackground(getActivity(), null);
}
else if (item instanceof PhotoItem) {
mBackgroundHelper.setBackground( mBackgroundHelper.setBackground(
getActivity(), ((PhotoItem) item).getImageResourceId()); getActivity(), ((PhotoItem) item).getImageResourceId());
} }
@@ -85,8 +88,6 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm
} }
}, 2000); }, 2000);
} }
mBackgroundHelper.attach(getActivity());
} }
private void setupRows() { private void setupRows() {
@@ -112,7 +113,7 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm
listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5)); listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5));
listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_6)); listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_6));
listRowAdapter.add(new PhotoItem("Android TV", "open RowsActivity", R.drawable.gallery_photo_7)); listRowAdapter.add(new PhotoItem("Android TV", "open RowsActivity", R.drawable.gallery_photo_7));
listRowAdapter.add(new PhotoItem("Leanback", "open MainActivity", R.drawable.gallery_photo_8)); listRowAdapter.add(new PhotoItem("Leanback", "open BrowseActivity", R.drawable.gallery_photo_8));
HeaderItem header = new HeaderItem(i, "Row " + i); HeaderItem header = new HeaderItem(i, "Row " + i);
mRowsAdapter.add(new ListRow(header, listRowAdapter)); mRowsAdapter.add(new ListRow(header, listRowAdapter));
} }
@@ -128,7 +129,7 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm
Intent intent; Intent intent;
Bundle bundle; Bundle bundle;
if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) { if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) {
intent = new Intent(getActivity(), MainActivity.class); intent = new Intent(getActivity(), BrowseActivity.class);
bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()) bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
.toBundle(); .toBundle();
} else if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) { } else if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) {

View File

@@ -35,4 +35,15 @@ public class DetailsActivity extends Activity
} }
} }
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
BackgroundHelper.attach(this);
}
@Override
public void onStop() {
BackgroundHelper.release(this);
super.onStop();
}
} }

View File

@@ -159,8 +159,6 @@ public class DetailsFragment extends android.support.v17.leanback.app.DetailsFra
prepareEntranceTransition(); prepareEntranceTransition();
} }
} }
mBackgroundHelper.attach(getActivity());
} }
@Override @Override
@@ -206,4 +204,13 @@ public class DetailsFragment extends android.support.v17.leanback.app.DetailsFra
setAdapter(mRowsAdapter); setAdapter(mRowsAdapter);
} }
@Override
public void onStart() {
super.onStart();
if (mPhotoItem != null) {
mBackgroundHelper.setBackground(
getActivity(), mPhotoItem.getImageResourceId());
}
}
} }

View File

@@ -88,5 +88,4 @@ public class MainActivity extends Activity {
} }
} }
} }