diff options
Diffstat (limited to 'src/org/cyanogenmod')
9 files changed, 424 insertions, 74 deletions
diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseActivity.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseActivity.java index 973618d..72f5b97 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseActivity.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseActivity.java @@ -67,7 +67,7 @@ public class PhotoPhaseActivity extends Activity implements OnTouchListener { mGLSurfaceView = new GLSurfaceView(getApplicationContext()); mGLSurfaceView.setEGLContextClientVersion(2); mGLSurfaceView.setEGLConfigChooser(false); - mRenderer = new PhotoPhaseRenderer(this, new GLESSurfaceDispatcher(mGLSurfaceView)); + mRenderer = new PhotoPhaseRenderer(this, new GLESSurfaceDispatcher(mGLSurfaceView), false); mGLSurfaceView.setRenderer(mRenderer); mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); mGLSurfaceView.setPreserveEGLContextOnPause(preserveEglCtx); diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseRenderer.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseRenderer.java index c307023..c4950a3 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseRenderer.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseRenderer.java @@ -62,12 +62,19 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { private final long mInstance; private static long sInstances; + private final boolean mIsPreview; + boolean mIsPaused; + boolean mRecreateWorld; + final Context mContext; EffectContext mEffectContext; private final Handler mHandler; final GLESSurfaceDispatcher mDispatcher; TextureManager mTextureManager; + final AlarmManager mAlarmManager; + PendingIntent mRecreateDispositionPendingIntent; + PhotoPhaseWallpaperWorld mWorld; ColorShape mOverlay; OopsShape mOopsShape; @@ -86,7 +93,7 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { private final float[] mProjMatrix = new float[16]; private final float[] mVMatrix = new float[16]; - private final Object mDrawing = new Object(); + final Object mDrawing = new Object(); final Object mMediaSync = new Object(); private PendingIntent mMediaScanIntent; @@ -95,48 +102,60 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { @Override public void onReceive(Context context, Intent intent) { // Check what flags are been requested - boolean recreateWorld = intent.getBooleanExtra(PreferencesProvider.EXTRA_FLAG_RECREATE_WORLD, false); + boolean recreateWorld = intent.getBooleanExtra( + PreferencesProvider.EXTRA_FLAG_RECREATE_WORLD, false); boolean redraw = intent.getBooleanExtra(PreferencesProvider.EXTRA_FLAG_REDRAW, false); - boolean emptyTextureQueue = intent.getBooleanExtra(PreferencesProvider.EXTRA_FLAG_EMPTY_TEXTURE_QUEUE, false); - boolean mediaReload = intent.getBooleanExtra(PreferencesProvider.EXTRA_FLAG_MEDIA_RELOAD, false); - boolean mediaIntervalChanged = intent.getBooleanExtra(PreferencesProvider.EXTRA_FLAG_MEDIA_INTERVAL_CHANGED, false); + boolean emptyTextureQueue = intent.getBooleanExtra( + PreferencesProvider.EXTRA_FLAG_EMPTY_TEXTURE_QUEUE, false); + boolean mediaReload = intent.getBooleanExtra( + PreferencesProvider.EXTRA_FLAG_MEDIA_RELOAD, false); + boolean mediaIntervalChanged = intent.getBooleanExtra( + PreferencesProvider.EXTRA_FLAG_MEDIA_INTERVAL_CHANGED, false); + int dispositionInterval = intent.getIntExtra( + PreferencesProvider.EXTRA_FLAG_DISPOSITION_INTERVAL_CHANGED, -1); + + // Empty texture queue? if (emptyTextureQueue) { if (mTextureManager != null) { mTextureManager.emptyTextureQueue(true); } } + + // Media reload. Purging resources and performs a media query if (mediaReload) { synchronized (mMediaSync) { if (mTextureManager != null) { - boolean userReloadRequest = - intent.getBooleanExtra( - PreferencesProvider.EXTRA_ACTION_MEDIA_USER_RELOAD_REQUEST, false); + boolean userReloadRequest = intent.getBooleanExtra( + PreferencesProvider.EXTRA_ACTION_MEDIA_USER_RELOAD_REQUEST, false); mTextureManager.reloadMedia(userReloadRequest); scheduleOrCancelMediaScan(); } } } + + // Media scan interval was changed. Reschedule if (mediaIntervalChanged) { scheduleOrCancelMediaScan(); } + + // Media scan interval was changed. Reschedule + if (dispositionInterval != -1) { + scheduleDispositionRecreation(); + } + + // Recreate the whole world? if (recreateWorld && mWorld != null) { - // Recreate the wallpaper world - try { - mWorld.recreateWorld(mWidth, mMeasuredHeight); - } catch (GLException e) { - Log.e(TAG, "Cannot recreate the wallpaper world.", e); - } + recreateWorld(); } + + // Performs a redraw? if (redraw) { - mDispatcher.requestRender(); + forceRedraw(); } } }; private final Runnable mTransitionThread = new Runnable() { - /** - * {@inheritDoc} - */ @Override public void run() { // Run in GLES's thread @@ -144,12 +163,14 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { @Override public void run() { try { - // Select a new transition - mWorld.selectRandomTransition(); - mLastRunningTransition = System.currentTimeMillis(); - - // Now force continuously render while transition is applied - mDispatcher.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); + if (!mIsPaused) { + // Select a new transition + mWorld.selectRandomTransition(); + mLastRunningTransition = System.currentTimeMillis(); + + // Now force continuously render while transition is applied + mDispatcher.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); + } } catch (Throwable ex) { Log.e(TAG, "Something was wrong selecting the transition", ex); } @@ -163,14 +184,19 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { * * @param ctx The current context * @param dispatcher The GLES dispatcher + * @param isPreview Indicates if the renderer is in preview mode */ - public PhotoPhaseRenderer(Context ctx, GLESSurfaceDispatcher dispatcher) { + public PhotoPhaseRenderer(Context ctx, GLESSurfaceDispatcher dispatcher, boolean isPreview) { super(); mContext = ctx; mHandler = new Handler(); mDispatcher = dispatcher; mInstance = sInstances; + mIsPreview = isPreview; + mIsPaused = true; + mRecreateWorld = false; sInstances++; + mAlarmManager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE); } /** @@ -256,6 +282,7 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { */ public void onPause() { if (DEBUG) Log.d(TAG, "onPause [" + mInstance + "]"); + mIsPaused = true; mHandler.removeCallbacks(mTransitionThread); if (mTextureManager != null) { mTextureManager.setPause(true); @@ -270,6 +297,13 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { if (mTextureManager != null) { mTextureManager.setPause(false); } + mIsPaused = false; + if (mRecreateWorld) { + recreateWorld(); + } else { + mDispatcher.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); + mDispatcher.requestRender(); + } } /** @@ -334,7 +368,7 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { mContext.startActivity(intent); } } catch (ActivityNotFoundException ex) { - Log.e(TAG, "Open activity not found for " + frame.getTextureInfo().path, ex); + Log.e(TAG, "Open action not found for " + frame.getTextureInfo().path, ex); } } else if (touchAction.compareTo(TouchAction.SHARE) == 0) { @@ -350,7 +384,7 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { mContext.startActivity(intent); } } catch (ActivityNotFoundException ex) { - Log.e(TAG, "Send activity not found for " + frame.getTextureInfo().path, ex); + Log.e(TAG, "Send action not found for " + frame.getTextureInfo().path, ex); } } } @@ -390,6 +424,11 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { } void scheduleOrCancelMediaScan() { + // Ignored in preview mode + if (mIsPreview) { + return; + } + int interval = Preferences.Media.getRefreshFrecuency(); if (interval != Preferences.Media.MEDIA_RELOAD_DISABLED) { scheduleMediaScan(interval); @@ -404,14 +443,19 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { * @param interval The new interval */ private void scheduleMediaScan(int interval) { - AlarmManager am = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); + // Ignored in preview mode + if (mIsPreview) { + return; + } Intent i = new Intent(PreferencesProvider.ACTION_SETTINGS_CHANGED); i.putExtra(PreferencesProvider.EXTRA_FLAG_MEDIA_RELOAD, Boolean.TRUE); - mMediaScanIntent = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); + mMediaScanIntent = PendingIntent.getBroadcast( + mContext, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); long milliseconds = Preferences.Media.getRefreshFrecuency() * 1000L; - am.set(AlarmManager.RTC, System.currentTimeMillis() + milliseconds, mMediaScanIntent); + long nextTime = System.currentTimeMillis() + milliseconds; + mAlarmManager.set(AlarmManager.RTC, nextTime, mMediaScanIntent); } /** @@ -419,13 +463,93 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { */ private void cancelMediaScan() { if (mMediaScanIntent != null) { - AlarmManager am = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); - am.cancel(mMediaScanIntent); + mAlarmManager.cancel(mMediaScanIntent); mMediaScanIntent = null; } } /** + * Method that schedule a new recreation of the current disposition + */ + void scheduleDispositionRecreation() { + // Ignored in preview mode + if (mIsPreview) { + return; + } + + // Cancel current alarm + cancelDispositionRecreation(); + + // Is random disposition enabled? + if (!Preferences.Layout.isRandomDispositions()) { + return; + } + + // Schedule the next recreation if interval has been configured + int interval = Preferences.Layout.getRandomDispositionsInterval(); + if (interval > 0) { + // Created the intent + Intent intent = new Intent(PreferencesProvider.ACTION_SETTINGS_CHANGED); + intent.putExtra(PreferencesProvider.EXTRA_FLAG_RECREATE_WORLD, Boolean.TRUE); + mRecreateDispositionPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); + + // Schedule the pending intent + long nextTime = System.currentTimeMillis() + interval; + mAlarmManager.set(AlarmManager.RTC, nextTime, mRecreateDispositionPendingIntent); + } + } + + /** + * Method that cancels a pending media scan + */ + private void cancelDispositionRecreation() { + // Cancel current alarm + if (mRecreateDispositionPendingIntent != null) { + mAlarmManager.cancel(mRecreateDispositionPendingIntent); + } + } + + /** + * Recreate the world + */ + void recreateWorld() { +System.out.println("recreateWorld(): " + mIsPaused); + if (mIsPaused) { + mRecreateWorld = true; + return; + } + + // Recreate the wallpaper world (under a GLES context) + mDispatcher.dispatch(new Runnable() { + @Override + public void run() { + synchronized (mDrawing) { + try { + mLastRunningTransition = 0; + mWorld.recreateWorld(mWidth, mMeasuredHeight); + } catch (GLException e) { + Log.e(TAG, "Cannot recreate the wallpaper world.", e); + } finally { + mDispatcher.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); + mDispatcher.requestRender(); + } + scheduleDispositionRecreation(); + } + } + }); + } + + /** + * Force a redraw of the screen + */ + void forceRedraw() { + if (!mIsPaused) { + mDispatcher.requestRender(); + } + } + + /** * Method that destroy all the internal references */ private void recycle() { @@ -500,6 +624,9 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { } else { mTextureManager.updateEffectContext(mEffectContext); } + + // Schedule dispositions random recreation (if need it) + scheduleDispositionRecreation(); } /** @@ -585,14 +712,14 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { // Draw the background drawBackground(); - if (mWorld != null) { + if (!mIsPaused && mWorld != null) { // Now draw the world (all the photo frames with effects) mWorld.draw(mMVPMatrix); // Check if we have some pending transition or transition has // exceed its timeout if (Preferences.General.Transitions.getTransitionInterval() > 0) { - if (!mWorld.hasRunningTransition() || firedTransitionTimeout()) { + if (!mWorld.hasRunningTransition() || isTransitionTimeoutFired()) { mDispatcher.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // Now start a delayed thread to generate the next effect @@ -603,14 +730,16 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { } } else { // Just display the initial frames and never make transitions - if (!mWorld.hasRunningTransition() || firedTransitionTimeout()) { + if (!mWorld.hasRunningTransition() || isTransitionTimeoutFired()) { mDispatcher.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } } - - // Draw the overlay - drawOverlay(); + } else if (mIsPaused) { + mDispatcher.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } + + // Draw the overlay + drawOverlay(); } } @@ -622,7 +751,7 @@ public class PhotoPhaseRenderer implements GLSurfaceView.Renderer { * * @return boolean if the transition has exceed the timeout */ - private boolean firedTransitionTimeout() { + private boolean isTransitionTimeoutFired() { long now = System.currentTimeMillis(); long diff = now - mLastRunningTransition; return mLastRunningTransition != 0 && diff > Transition.MAX_TRANSTION_TIME; diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java index 5152bc8..e218f11 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java @@ -227,7 +227,8 @@ public class PhotoPhaseWallpaper @Override public Renderer getNewRenderer(GLSurfaceView view) { if (DEBUG) Log.d(TAG, "getNewRenderer()"); - PhotoPhaseRenderer renderer = new PhotoPhaseRenderer(this, new GLESSurfaceDispatcher(view)); + PhotoPhaseRenderer renderer = new PhotoPhaseRenderer(this, + new GLESSurfaceDispatcher(view), mEngine.isPreview()); renderer.onCreate(); mRenderers.add(renderer); if (DEBUG) Log.d(TAG, "renderer" + renderer); diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java index 0310b58..45d3d74 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java @@ -18,6 +18,7 @@ package org.cyanogenmod.wallpapers.photophase; import android.content.Context; import android.content.res.Configuration; +import android.content.res.Resources; import android.graphics.PointF; import android.graphics.RectF; import android.util.Log; @@ -27,6 +28,7 @@ import org.cyanogenmod.wallpapers.photophase.preferences.PreferencesProvider.Pre import org.cyanogenmod.wallpapers.photophase.transitions.Transition; import org.cyanogenmod.wallpapers.photophase.transitions.Transitions; import org.cyanogenmod.wallpapers.photophase.transitions.Transitions.TRANSITIONS; +import org.cyanogenmod.wallpapers.photophase.utils.DispositionUtil; import org.cyanogenmod.wallpapers.photophase.utils.Utils; import java.util.ArrayList; @@ -60,6 +62,9 @@ public class PhotoPhaseWallpaperWorld { private boolean mRecycled; + private String[] mPortraitDispositions; + private String[] mLandscapeDispositions; + /** * Constructor <code>PhotoPhaseWallpaperWorld</code> * @@ -74,6 +79,10 @@ public class PhotoPhaseWallpaperWorld { mCurrent = -1; mUnusedTransitions = new ArrayList<Transition>(); mRecycled = false; + + Resources res = ctx.getResources(); + mPortraitDispositions = res.getStringArray(R.array.portrait_disposition_templates); + mLandscapeDispositions = res.getStringArray(R.array.landscape_disposition_templates); } /** @@ -200,7 +209,8 @@ public class PhotoPhaseWallpaperWorld { mUnusedTransitions.add(currentTransition); if (finalTarget != null) { - Transition transition = getOrCreateTransition(TRANSITIONS.NO_TRANSITION, finalTarget); + Transition transition = getOrCreateTransition( + TRANSITIONS.NO_TRANSITION, finalTarget); mTransitions.set(mCurrent, transition); currentTarget.recycle(); @@ -287,11 +297,11 @@ public class PhotoPhaseWallpaperWorld { int rows = portrait ? Preferences.Layout.getRows() : Preferences.Layout.getCols(); float cellw = 2.0f / cols; float cellh = 2.0f / rows; - List<Disposition> dispositions = portrait - ? Preferences.Layout.getPortraitDisposition() - : Preferences.Layout.getLandscapeDisposition(); - if (DEBUG) Log.d(TAG, - "Dispositions: " + dispositions.size() + " | " + String.valueOf(dispositions)); + List<Disposition> dispositions = getWorldDispositions(portrait); + if (DEBUG) { + Log.d(TAG, "Dispositions: " + dispositions.size() + " | " + + String.valueOf(dispositions)); + } mPhotoFrames = new ArrayList<PhotoFrame>(dispositions.size()); mTransitions = new ArrayList<Transition>(dispositions.size()); mTransitionsQueue = new ArrayList<Integer>(dispositions.size()); @@ -300,7 +310,8 @@ public class PhotoPhaseWallpaperWorld { for (Disposition disposition : dispositions) { // Create the photo frame float[] frameVertices = getVerticesFromDisposition(disposition, cellw, cellh); - float[] photoVertices = getFramePadding(frameVertices, portrait ? w : h, portrait ? h : w); + float[] photoVertices = getFramePadding(frameVertices, + portrait ? w : h, portrait ? h : w); PhotoFrame frame = new PhotoFrame( mContext, @@ -416,4 +427,29 @@ public class PhotoPhaseWallpaperWorld { paddingCoords[7] -= pxh; return paddingCoords; } + + /** + * Method that returns the dispositions to draw in the world + * + * @param portrait If the orientation is portrait (true) or landscape (false) + * @return List<Disposition> The list of dispositions + */ + private List<Disposition> getWorldDispositions(boolean portrait) { + // If user selected a random disposition, then use one of the predefined layouts + if (Preferences.Layout.isRandomDispositions()) { + // Random + if (portrait) { + // Portrait + int next = Utils.getNextRandom(0, mPortraitDispositions.length -1); + return DispositionUtil.toDispositions(mPortraitDispositions[next]); + } + // Landscape + int next = Utils.getNextRandom(0, mLandscapeDispositions.length -1); + return DispositionUtil.toDispositions(mLandscapeDispositions[next]); + } + // User-defined + return portrait + ? Preferences.Layout.getPortraitDisposition() + : Preferences.Layout.getLandscapeDisposition(); + } } diff --git a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java index 3731a1b..bfa1edc 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java +++ b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java @@ -48,7 +48,7 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { private static final String TAG = "TextureManager"; - private static final int QUEUE_SIZE = 3; + private static final int QUEUE_SIZE = 8; final Context mContext; final Handler mHandler; diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java index 00d75a6..262002c 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java @@ -143,7 +143,7 @@ public class ChoosePicturesFragment extends PreferenceFragment */ @Override protected void onProgressUpdate(Album... albums) { - for(Album album : albums) { + for (Album album : albums) { mAlbums.add(album); } mAlbumAdapter.notifyDataSetChanged(); diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java index 4dd6da8..04c6571 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java @@ -50,7 +50,7 @@ public class GeneralPreferenceFragment extends PreferenceFragment { private ListPreference mTouchActions; private CheckBoxPreference mFixAspectRatio; private MultiSelectListPreference mTransitionsTypes; - private SeekBarProgressPreference mTransitionsInterval; + SeekBarProgressPreference mTransitionsInterval; private MultiSelectListPreference mEffectsTypes; boolean mRedrawFlag; @@ -109,6 +109,10 @@ public class GeneralPreferenceFragment extends PreferenceFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + final String formatSeconds = getString(R.string.format_seconds); + final String formatMinutes = getString(R.string.format_minutes); + final String formatDim = getString(R.string.format_dim); + // Change the preference manager getPreferenceManager().setSharedPreferencesName(PreferencesProvider.PREFERENCES_FILE); getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE); @@ -120,7 +124,7 @@ public class GeneralPreferenceFragment extends PreferenceFragment { addPreferencesFromResource(R.xml.preferences_general); mWallpaperDim = (SeekBarProgressPreference)findPreference("ui_wallpaper_dim"); - mWallpaperDim.setFormat(getString(R.string.pref_general_settings_wallpaper_dim_format)); + mWallpaperDim.setFormat(formatDim); mWallpaperDim.setOnPreferenceChangeListener(mOnChangeListener); mBackgroundColor = (ColorPickerPreference)findPreference("ui_background_color"); @@ -137,7 +141,7 @@ public class GeneralPreferenceFragment extends PreferenceFragment { final int[] transitionsIntervals = res.getIntArray(R.array.transitions_intervals_values); mTransitionsInterval = (SeekBarProgressPreference)findPreference("ui_transition_interval"); - mTransitionsInterval.setFormat(getString(R.string.pref_general_transitions_interval_format)); + mTransitionsInterval.setFormat(getString(R.string.format_seconds)); mTransitionsInterval.setMax(transitionsIntervals.length - 1); int transitionInterval = prefs.getInt("ui_transition_interval", Preferences.General.Transitions.DEFAULT_TRANSITION_INTERVAL_INDEX); @@ -148,7 +152,14 @@ public class GeneralPreferenceFragment extends PreferenceFragment { mTransitionsInterval.setOnDisplayProgress(new OnDisplayProgress() { @Override public String onDisplayProgress(int progress) { - return String.valueOf(transitionsIntervals[progress] / 1000); + if (transitionsIntervals[progress] < 60000) { + // Seconds + mTransitionsInterval.setFormat(formatSeconds); + return String.valueOf(transitionsIntervals[progress] / 1000); + } + // Minutes + mTransitionsInterval.setFormat(formatMinutes); + return String.valueOf(transitionsIntervals[progress] / 1000 / 60); } }); mTransitionsInterval.setOnPreferenceChangeListener(mOnChangeListener); diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/LayoutPreferenceFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/LayoutPreferenceFragment.java index 564c425..226fb43 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/LayoutPreferenceFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/LayoutPreferenceFragment.java @@ -17,16 +17,76 @@ package org.cyanogenmod.wallpapers.photophase.preferences; import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Resources; import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.Preference; import android.preference.PreferenceFragment; +import android.preference.Preference.OnPreferenceChangeListener; +import android.util.Log; import org.cyanogenmod.wallpapers.photophase.R; +import org.cyanogenmod.wallpapers.photophase.preferences.PreferencesProvider.Preferences; +import org.cyanogenmod.wallpapers.photophase.preferences.SeekBarProgressPreference.OnDisplayProgress; /** * A fragment class with the layout disposition */ public class LayoutPreferenceFragment extends PreferenceFragment { + private static final String TAG = "LayoutPreferenceFragment"; + + private static final boolean DEBUG = false; + + private CheckBoxPreference mRandomDispositions; + SeekBarProgressPreference mRandomDispositionsInterval; + Preference mPortraitDisposition; + Preference mLandscapeDisposition; + + boolean mRedrawFlag; + boolean mDispositionIntervalFlag; + + private final OnPreferenceChangeListener mOnChangeListener = new OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String key = preference.getKey(); + if (DEBUG) Log.d(TAG, "Preference changed: " + key + "=" + newValue); + if (key.compareTo("ui_disposition_random") == 0) { + boolean randomDispositions = ((Boolean)newValue).booleanValue(); + mPortraitDisposition.setEnabled(!randomDispositions); + mLandscapeDisposition.setEnabled(!randomDispositions); + mRedrawFlag = true; + } else if (key.compareTo("ui_disposition_random_interval") == 0) { + mDispositionIntervalFlag = true; + } + return true; + } + }; + + /** + * {@inheritDoc} + */ + @Override + public void onDestroy() { + super.onDestroy(); + + // Reload the settings + PreferencesProvider.reload(getActivity()); + + // Notify that the settings was changed + Intent intent = new Intent(PreferencesProvider.ACTION_SETTINGS_CHANGED); + if (mRedrawFlag) { + intent.putExtra(PreferencesProvider.EXTRA_FLAG_REDRAW, Boolean.TRUE); + } + if (mDispositionIntervalFlag) { + int interval = Preferences.Layout.getRandomDispositionsInterval(); + intent.putExtra(PreferencesProvider.EXTRA_FLAG_DISPOSITION_INTERVAL_CHANGED, interval); + } + getActivity().sendBroadcast(intent); + } + /** * {@inheritDoc} */ @@ -34,11 +94,66 @@ public class LayoutPreferenceFragment extends PreferenceFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + final String formatSeconds = getString(R.string.format_seconds); + final String formatMinutes = getString(R.string.format_minutes); + final String formatHours = getString(R.string.format_hours); + final String formatDays = getString(R.string.format_days); + // Change the preference manager getPreferenceManager().setSharedPreferencesName(PreferencesProvider.PREFERENCES_FILE); getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE); + final SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); + final Resources res = getActivity().getResources(); + // Add the preferences addPreferencesFromResource(R.xml.preferences_layout); + + // -- Random dispositions + mRandomDispositions = (CheckBoxPreference)findPreference("ui_disposition_random"); + mRandomDispositions.setOnPreferenceChangeListener(mOnChangeListener); + + // -- Interval + final int[] randomDispositionsIntervals = + res.getIntArray(R.array.random_dispositions_intervals_values); + mRandomDispositionsInterval = + (SeekBarProgressPreference)findPreference("ui_disposition_random_interval"); + mRandomDispositionsInterval.setMax(randomDispositionsIntervals.length - 1); + int transitionInterval = prefs.getInt("ui_disposition_random_interval", + Preferences.Layout.DEFAULT_RANDOM_DISPOSITIONS_INTERVAL_INDEX); + if (transitionInterval > (randomDispositionsIntervals.length - 1)) { + mRandomDispositionsInterval.setProgress( + Preferences.Layout.DEFAULT_RANDOM_DISPOSITIONS_INTERVAL_INDEX); + } + mRandomDispositionsInterval.setOnDisplayProgress(new OnDisplayProgress() { + @Override + public String onDisplayProgress(int progress) { + if (randomDispositionsIntervals[progress] < 60000) { + // Seconds + mRandomDispositionsInterval.setFormat(formatSeconds); + return String.valueOf(randomDispositionsIntervals[progress] / 1000); + } else if (randomDispositionsIntervals[progress] < 3600000) { + // Minutes + mRandomDispositionsInterval.setFormat(formatMinutes); + return String.valueOf(randomDispositionsIntervals[progress] / 1000 / 60); + } else if (randomDispositionsIntervals[progress] < 86400000) { + // Hours + mRandomDispositionsInterval.setFormat(formatHours); + return String.valueOf(randomDispositionsIntervals[progress] / 1000 / 60 / 60); + } + // Days + mRandomDispositionsInterval.setFormat(formatDays); + return String.valueOf(randomDispositionsIntervals[progress] / 1000 / 60 / 60 / 24); + } + }); + mRandomDispositionsInterval.setOnPreferenceChangeListener(mOnChangeListener); + + // -- Portrait + mPortraitDisposition = findPreference("ui_disposition_portrait"); + mPortraitDisposition.setEnabled(!Preferences.Layout.isRandomDispositions()); + + // -- Landscape + mLandscapeDisposition = findPreference("ui_disposition_landscape"); + mLandscapeDisposition.setEnabled(!Preferences.Layout.isRandomDispositions()); } } diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java index 0edc8b7..d1b2056 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java @@ -45,10 +45,12 @@ public final class PreferencesProvider { * @see #EXTRA_FLAG_REDRAW * {@hide} */ - public static final String ACTION_SETTINGS_CHANGED = "org.cyanogenmod.wallpapers.photophase.actions.SETTINGS_CHANGED"; + public static final String ACTION_SETTINGS_CHANGED = + "org.cyanogenmod.wallpapers.photophase.actions.SETTINGS_CHANGED"; /** - * An extra setting that indicates that the changed setting request a whole recreation of the wallpaper world + * An extra setting that indicates that the changed setting request a whole recreation + * of the wallpaper world * {@hide} */ public static final String EXTRA_FLAG_RECREATE_WORLD = "flag_recreate_world"; @@ -84,7 +86,16 @@ public final class PreferencesProvider { * @see #EXTRA_FLAG_MEDIA_RELOAD * {@hide} */ - public static final String EXTRA_ACTION_MEDIA_USER_RELOAD_REQUEST = "action_media_user_reload_req"; + public static final String EXTRA_ACTION_MEDIA_USER_RELOAD_REQUEST = + "action_media_user_reload_req"; + + /** + * An extra setting that indicates that the changed setting changed the disposition + * interval time. Contains the new interval time + * {@hide} + */ + public static final String EXTRA_FLAG_DISPOSITION_INTERVAL_CHANGED = + "flag_disposition_interval_changed"; /** * The shared preferences file @@ -97,6 +108,10 @@ public final class PreferencesProvider { * @hide */ static int[] TRANSITIONS_INTERVALS; + /** + * @hide + */ + static int[] RANDOM_DISPOSITIONS_INTERVALS; /** * Method that loads the all the preferences of the application @@ -110,6 +125,8 @@ public final class PreferencesProvider { final Resources res = context.getResources(); TRANSITIONS_INTERVALS = res.getIntArray(R.array.transitions_intervals_values); + RANDOM_DISPOSITIONS_INTERVALS = res.getIntArray( + R.array.random_dispositions_intervals_values); } /** @@ -306,7 +323,8 @@ public final class PreferencesProvider { * @return int The interval in seconds between updates. 0 means that updates are disabled */ public static int getRefreshFrecuency() { - return Integer.valueOf(getString("ui_media_refresh_interval", String.valueOf(MEDIA_RELOAD_DISABLED))); + return Integer.valueOf(getString("ui_media_refresh_interval", + String.valueOf(MEDIA_RELOAD_DISABLED))); } /** @@ -334,7 +352,8 @@ public final class PreferencesProvider { * @param context The current context * @param selection The new list of albums and pictures to be displayed */ - public static synchronized void setSelectedMedia(Context context, Set<String> selection) { + public static synchronized void setSelectedMedia( + Context context, Set<String> selection) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); Editor editor = preferences.edit(); @@ -351,7 +370,8 @@ public final class PreferencesProvider { */ public static Set<String> getLastDiscorevedAlbums() { // FIXME Typo. Remove when version 1005 is obsolete and unused - Set<String> oldKey = getStringSet("media_last_disvored_albums", new HashSet<String>()); + Set<String> oldKey = getStringSet("media_last_disvored_albums", + new HashSet<String>()); if (oldKey.size() > 0) { return oldKey; } @@ -365,7 +385,8 @@ public final class PreferencesProvider { * @param context The current context * @param albums The albums seen by the last media discovery scan */ - public static synchronized void setLastDiscorevedAlbums(Context context, Set<String> albums) { + public static synchronized void setLastDiscorevedAlbums( + Context context, Set<String> albums) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); Editor editor = preferences.edit(); @@ -384,11 +405,26 @@ public final class PreferencesProvider { private static final int DEFAULT_COLS = 4; private static final int DEFAULT_ROWS = 7; - public static final String DEFAULT_PORTRAIT_DISPOSITION = "0x0:2x1|0x2:1x3|0x4:3x6|2x2:3x3|3x0:3x0|3x1:3x1"; - public static final String DEFAULT_LANDSCAPE_DISPOSITION = "0x0:2x3|3x0:5x1|3x2:4x3|5x2:6x3|6x0:6x0|6x1:6x1"; + private static final boolean DEFAULT_RANDOM_DISPOSITION = false; + + /** + * The default portrait disposition + */ + public static final String DEFAULT_PORTRAIT_DISPOSITION = + "0x0:2x1|0x2:1x3|0x4:3x6|2x2:3x3|3x0:3x0|3x1:3x1"; + /** + * The default landscape disposition + */ + public static final String DEFAULT_LANDSCAPE_DISPOSITION = + "0x0:2x3|3x0:5x1|3x2:4x3|5x2:6x3|6x0:6x0|6x1:6x1"; + + /** + * The default transition interval + */ + public static final int DEFAULT_RANDOM_DISPOSITIONS_INTERVAL_INDEX = 0; /** - * Method that returns the rows of the wallpaper. + * Returns the number of rows of the wallpaper. * * @return int The rows of the wallpaper */ @@ -397,7 +433,7 @@ public final class PreferencesProvider { } /** - * Method that returns the columns of the wallpaper. + * Returns the number columns of the wallpaper. * * @return int The columns of the wallpaper */ @@ -406,9 +442,29 @@ public final class PreferencesProvider { } /** + * Returns if the device should generate random dispositions + * + * @return boolean If the system should generate random dispositions + */ + public static boolean isRandomDispositions() { + return getBoolean("ui_disposition_random", DEFAULT_RANDOM_DISPOSITION); + } + + /** + * Method that returns how often the random dispositions are triggered. + * + * @return int The milliseconds in which the next transition will be triggered + */ + public static int getRandomDispositionsInterval() { + int interval = getInt("ui_disposition_random_interval", + DEFAULT_RANDOM_DISPOSITIONS_INTERVAL_INDEX); + return RANDOM_DISPOSITIONS_INTERVALS[interval]; + } + + /** * Returns the disposition of the photo frames in the wallpaper on portrait screen. The - * setting is stored as <code>0x0:1x2|2x2:3x4|...</code>, which it means (position x=0, y=0, - * 1 cells width, 2 cells height, ...). + * setting is stored as <code>0x0:1x2|2x2:3x4|...</code>, which it means + * (position x=0, y=0, 1 cells width, 2 cells height, ...). * * @return List<Disposition> The photo frames dispositions */ @@ -423,7 +479,8 @@ public final class PreferencesProvider { * @param context The current context * @param dispositions The photo frames dispositions */ - public static void setPortraitDisposition(Context context, List<Disposition> dispositions) { + public static void setPortraitDisposition(Context context, + List<Disposition> dispositions) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); Editor editor = preferences.edit(); @@ -434,15 +491,15 @@ public final class PreferencesProvider { } /** - * Returns the disposition of the photo frames in the wallpaper on landscape screen. The - * setting is stored as <code>0x0:1x2|2x2:3x4|...</code>, which it means (position x=0, y=0, - * 1 cells width, 2 cells height, ...). + * Returns the disposition of the photo frames in the wallpaper on landscape screen. + * The setting is stored as <code>0x0:1x2|2x2:3x4|...</code>, which it means + * (position x=0, y=0, 1 cells width, 2 cells height, ...). * * @return List<Disposition> The photo frames dispositions */ public static List<Disposition> getLandscapeDisposition() { - return DispositionUtil.toDispositions( - getString("ui_layout_landscape_disposition", DEFAULT_LANDSCAPE_DISPOSITION)); + return DispositionUtil.toDispositions(getString("ui_layout_landscape_disposition", + DEFAULT_LANDSCAPE_DISPOSITION)); } /** @@ -451,7 +508,8 @@ public final class PreferencesProvider { * @param context The current context * @param dispositions The photo frames dispositions */ - public static void setLandscapeDisposition(Context context, List<Disposition> dispositions) { + public static void setLandscapeDisposition(Context context, + List<Disposition> dispositions) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); Editor editor = preferences.edit(); |