Back to project page Muzei.
The source code is released under:
Apache License
If you think the Android project Muzei listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2014 Google Inc.// w w w . j a v a 2 s . c o m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.apps.muzei.render; import android.content.Context; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import com.google.android.apps.muzei.event.BlurAmountChangedEvent; import com.google.android.apps.muzei.event.DimAmountChangedEvent; import com.google.android.apps.muzei.event.GreyAmountChangedEvent; import de.greenrobot.event.EventBus; public abstract class RenderController { protected Context mContext; protected MuzeiBlurRenderer mRenderer; protected Callbacks mCallbacks; protected boolean mVisible; private BitmapRegionLoader mQueuedBitmapRegionLoader; public RenderController(Context context, MuzeiBlurRenderer renderer, Callbacks callbacks) { mRenderer = renderer; mContext = context; mCallbacks = callbacks; EventBus.getDefault().register(this); } public void destroy() { if (mQueuedBitmapRegionLoader != null) { mQueuedBitmapRegionLoader.destroy(); } EventBus.getDefault().unregister(this); } public void onEventMainThread(BlurAmountChangedEvent e) { mRenderer.recomputeMaxPrescaledBlurPixels(); throttledForceReloadCurrentArtwork(); } public void onEventMainThread(DimAmountChangedEvent e) { mRenderer.recomputeMaxDimAmount(); throttledForceReloadCurrentArtwork(); } public void onEventMainThread(GreyAmountChangedEvent e) { mRenderer.recomputeGreyAmount(); throttledForceReloadCurrentArtwork(); } private void throttledForceReloadCurrentArtwork() { mThrottledForceReloadHandler.removeMessages(0); mThrottledForceReloadHandler.sendEmptyMessageDelayed(0, 250); } private Handler mThrottledForceReloadHandler = new Handler() { @Override public void handleMessage(Message msg) { reloadCurrentArtwork(true); } }; protected abstract BitmapRegionLoader openDownloadedCurrentArtwork(boolean forceReload); public void reloadCurrentArtwork(final boolean forceReload) { new AsyncTask<Void, Void, BitmapRegionLoader>() { @Override protected BitmapRegionLoader doInBackground(Void... voids) { // openDownloadedCurrentArtwork should be called on a background thread return openDownloadedCurrentArtwork(forceReload); } @Override protected void onPostExecute(final BitmapRegionLoader bitmapRegionLoader) { if (bitmapRegionLoader == null) { return; } mCallbacks.queueEventOnGlThread(new Runnable() { @Override public void run() { if (mVisible) { mRenderer.setAndConsumeBitmapRegionLoader(bitmapRegionLoader); } else { mQueuedBitmapRegionLoader = bitmapRegionLoader; } } }); } }.execute((Void) null); } public void setVisible(boolean visible) { mVisible = visible; if (visible) { mCallbacks.queueEventOnGlThread(new Runnable() { @Override public void run() { if (mQueuedBitmapRegionLoader != null) { mRenderer.setAndConsumeBitmapRegionLoader(mQueuedBitmapRegionLoader); mQueuedBitmapRegionLoader = null; } } }); mCallbacks.requestRender(); } } public static interface Callbacks { void queueEventOnGlThread(Runnable runnable); void requestRender(); } }