From a92b9d9f030cf1a9c74ac410a30a5d264743f51e Mon Sep 17 00:00:00 2001 From: wjiang Date: Fri, 14 Feb 2014 17:45:53 +0800 Subject: Gallery2: Fix batches of issues in movie effects Fix following issues: - All settings aren't restored to previous state when user cancel his operations. - Check the validity to avoid calling member function of an already released object. - Recycle effect instance when MovieActivity pauses. - Don't restore strength from pref when toggling switch. - Force the Effect's dialog to be uncancelable. - Fix null pointer and effect leak issue due to CMCC features. - Fix audio effects dialog display on small screens - Recognize bluetooth headset. Change-Id: I803897508c0c1a6723170b7691e3ece03680e4f1 --- AndroidManifest.xml | 1 + res/drawable-hdpi/knob.png | Bin 34246 -> 51511 bytes res/drawable-hdpi/knob_bg.png | Bin 14924 -> 0 bytes res/layout/audio_effects_dialog.xml | 4 +- src/com/android/gallery3d/app/MovieActivity.java | 74 +++++++++++++---------- src/com/android/gallery3d/app/MoviePlayer.java | 2 + src/com/android/gallery3d/ui/Knob.java | 25 +++++--- 7 files changed, 64 insertions(+), 42 deletions(-) delete mode 100644 res/drawable-hdpi/knob_bg.png diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 4e8a351c3..9e9f1de7a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -35,6 +35,7 @@ + diff --git a/src/com/android/gallery3d/app/MovieActivity.java b/src/com/android/gallery3d/app/MovieActivity.java index d22e56f82..325930746 100755 --- a/src/com/android/gallery3d/app/MovieActivity.java +++ b/src/com/android/gallery3d/app/MovieActivity.java @@ -71,6 +71,8 @@ import org.codeaurora.gallery3d.ext.MovieItem; import org.codeaurora.gallery3d.ext.MovieUtils; import org.codeaurora.gallery3d.video.ExtensionHelper; import org.codeaurora.gallery3d.video.MovieTitleHelper; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothProfile; /** * This activity plays a video from a specified URI. @@ -102,7 +104,6 @@ public class MovieActivity extends Activity { private boolean mVirtualizerSupported = false; private boolean mBassBoostSupported = false; - private SharedPreferences mPrefs; static enum Key { global_enabled, bb_strength, virt_strength }; @@ -114,6 +115,7 @@ public class MovieActivity extends Activity { private Knob mBassBoostKnob; private Knob mVirtualizerKnob; + private SharedPreferences mPrefs; private ShareActionProvider mShareProvider; private IMovieItem mMovieItem; private IActivityHooker mMovieHooker; @@ -142,10 +144,10 @@ public class MovieActivity extends Activity { || audioManager.isWiredHeadsetOn(); } } else if (action.equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { - mIsHeadsetOn = audioManager.isBluetoothA2dpOn() || audioManager.isWiredHeadsetOn(); + mIsHeadsetOn = false; } if (mEffectDialog != null) { - if (!mIsHeadsetOn && mEffectDialog.isShowing()) { + if (!mIsHeadsetOn && !isBtHeadsetConnected() && mEffectDialog.isShowing()) { mEffectDialog.dismiss(); showHeadsetPlugToast(); } @@ -182,6 +184,8 @@ public class MovieActivity extends Activity { initializeActionBar(intent); mFinishOnCompletion = intent.getBooleanExtra( MediaStore.EXTRA_FINISH_ON_COMPLETION, true); + mPrefs = getSharedPreferences(getApplicationContext().getPackageName(), + Context.MODE_PRIVATE); mPlayer = new MoviePlayer(rootView, this, mMovieItem, savedInstanceState, !mFinishOnCompletion) { @Override @@ -224,9 +228,6 @@ public class MovieActivity extends Activity { } } - mPrefs = getSharedPreferences(getApplicationContext().getPackageName(), - Context.MODE_PRIVATE); - mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { @@ -319,7 +320,7 @@ public class MovieActivity extends Activity { } private void onAudioEffectsMenuItemClick() { - if (!mIsHeadsetOn) { + if (!mIsHeadsetOn && !isBtHeadsetConnected()) { showHeadsetPlugToast(); } else { LayoutInflater factory = LayoutInflater.from(this); @@ -332,12 +333,12 @@ public class MovieActivity extends Activity { mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - mBassBoostEffect.setEnabled(isChecked); - mBassBoostEffect.setStrength((short) - mPrefs.getInt(Key.bb_strength.toString(), 0)); - mVirtualizerEffect.setEnabled(isChecked); - mVirtualizerEffect.setStrength((short) - mPrefs.getInt(Key.virt_strength.toString(), 0)); + if(mBassBoostEffect != null) { + mBassBoostEffect.setEnabled(isChecked); + } + if(mVirtualizerEffect != null) { + mVirtualizerEffect.setEnabled(isChecked); + } mBassBoostKnob.setEnabled(isChecked); mVirtualizerKnob.setEnabled(isChecked); } @@ -350,7 +351,9 @@ public class MovieActivity extends Activity { mBassBoostKnob.setOnKnobChangeListener(new Knob.OnKnobChangeListener() { @Override public void onValueChanged(Knob knob, int value, boolean fromUser) { - mBassBoostEffect.setStrength((short) value); + if(mBassBoostEffect != null) { + mBassBoostEffect.setStrength((short) value); + } } @Override @@ -374,7 +377,9 @@ public class MovieActivity extends Activity { mVirtualizerKnob.setOnKnobChangeListener(new Knob.OnKnobChangeListener() { @Override public void onValueChanged(Knob knob, int value, boolean fromUser) { - mVirtualizerEffect.setStrength((short) value); + if(mVirtualizerEffect != null) { + mVirtualizerEffect.setStrength((short) value); + } } @Override @@ -410,14 +415,19 @@ public class MovieActivity extends Activity { @Override public void onClick(DialogInterface dialog, int which) { boolean enabled = mPrefs.getBoolean(Key.global_enabled.toString(), false); - mBassBoostEffect.setStrength((short) - mPrefs.getInt(Key.bb_strength.toString(), 0)); - mBassBoostEffect.setEnabled(enabled); - mVirtualizerEffect.setStrength((short) - mPrefs.getInt(Key.virt_strength.toString(), 0)); - mVirtualizerEffect.setEnabled(enabled); + if(mBassBoostEffect != null) { + mBassBoostEffect.setStrength((short) + mPrefs.getInt(Key.bb_strength.toString(), 0)); + mBassBoostEffect.setEnabled(enabled); + } + if(mVirtualizerEffect != null) { + mVirtualizerEffect.setStrength((short) + mPrefs.getInt(Key.virt_strength.toString(), 0)); + mVirtualizerEffect.setEnabled(enabled); + } } }) + .setCancelable(false) .create(); mEffectDialog.show(); } @@ -433,7 +443,7 @@ public class MovieActivity extends Activity { mVirtualizerEffect = new Virtualizer(0, sessionId); } - if (mIsHeadsetOn) { + if (mIsHeadsetOn || isBtHeadsetConnected()) { if (mPrefs.getBoolean(Key.global_enabled.toString(), false)) { if (mBassBoostSupported) { mBassBoostEffect.setStrength((short) @@ -555,11 +565,11 @@ public class MovieActivity extends Activity { registerReceiver(mReceiver, intentFilter); } - initEffects(mPlayer.getAudioSessionId()); mResumed = true; if (!isKeyguardLocked() && !mControlResumed && mPlayer != null) { mPlayer.onResume(); mControlResumed = true; + //initEffects(mPlayer.getAudioSessionId()); } enhanceActionBar(); super.onResume(); @@ -575,6 +585,15 @@ public class MovieActivity extends Activity { } } + private boolean isBtHeadsetConnected() { + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + if ((BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) + || (BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.A2DP))) { + return true; + } + return false; + } + @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); @@ -583,15 +602,8 @@ public class MovieActivity extends Activity { @Override public void onDestroy() { + releaseEffects(); mPlayer.onDestroy(); - if (mBassBoostEffect != null) { - mBassBoostEffect.setEnabled(false); - mBassBoostEffect.release(); - } - if (mVirtualizerEffect != null) { - mVirtualizerEffect.setEnabled(false); - mVirtualizerEffect.release(); - } super.onDestroy(); mMovieHooker.onDestroy(); } diff --git a/src/com/android/gallery3d/app/MoviePlayer.java b/src/com/android/gallery3d/app/MoviePlayer.java index 1360bb3e8..66005f6a7 100644 --- a/src/com/android/gallery3d/app/MoviePlayer.java +++ b/src/com/android/gallery3d/app/MoviePlayer.java @@ -1109,6 +1109,7 @@ public class MoviePlayer implements mVideoView.stopPlayback(); mVideoView.setVisibility(View.INVISIBLE); clearVideoInfo(); + mActivityContext.releaseEffects(); mMovieItem = next; mActivityContext.refreshMovieInfo(mMovieItem); doStartVideo(false, 0, 0); @@ -1145,6 +1146,7 @@ public class MoviePlayer implements mVideoView.setVisibility(View.INVISIBLE); mVideoView.setVisibility(View.VISIBLE); clearVideoInfo(); + mActivityContext.releaseEffects(); mFirstBePlayed = false; mController.setCanReplay(true); mController.showEnded(); diff --git a/src/com/android/gallery3d/ui/Knob.java b/src/com/android/gallery3d/ui/Knob.java index f246ce59b..d16bf6110 100755 --- a/src/com/android/gallery3d/ui/Knob.java +++ b/src/com/android/gallery3d/ui/Knob.java @@ -90,10 +90,9 @@ public class Knob extends FrameLayout { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Knob, 0, 0); String label; - int background, foreground; + int foreground; try { label = a.getString(R.styleable.Knob_label); - background = a.getResourceId(R.styleable.Knob_background, R.drawable.knob_bg); foreground = a.getResourceId(R.styleable.Knob_foreground, R.drawable.knob); } finally { a.recycle(); @@ -108,7 +107,6 @@ public class Knob extends FrameLayout { mLowlightColor = res.getColor(R.color.lowlight); mDisabledColor = res.getColor(R.color.disabled); - setBackgroundResource(background); ((ImageView) findViewById(R.id.knob_foreground)).setImageResource(foreground); mLabelTV = (TextView) findViewById(R.id.knob_label); @@ -213,16 +211,25 @@ public class Knob extends FrameLayout { @Override protected void onSizeChanged(int w, int h, int oldW, int oldH) { - mWidth = w; + int size = w > h ? h : w; + mWidth = size; mIndicatorWidth = mKnobOn.getWidth(); - mRectF = new RectF(STROKE_WIDTH, STROKE_WIDTH, - mWidth - STROKE_WIDTH, mWidth - STROKE_WIDTH); + int diff; + if (w > h) { + diff = (w - h) / 2; + mRectF = new RectF(STROKE_WIDTH + diff, STROKE_WIDTH, + w - STROKE_WIDTH - diff, h - STROKE_WIDTH); + } else { + diff = (h - w) / 2; + mRectF = new RectF(STROKE_WIDTH, STROKE_WIDTH + diff, + w - STROKE_WIDTH, h - STROKE_WIDTH - diff); + } - mProgressTV.setTextSize(TypedValue.COMPLEX_UNIT_PX, w * 0.16f); - mProgressTV.setPadding(0, (int) (w * 0.33), 0, 0); + mProgressTV.setTextSize(TypedValue.COMPLEX_UNIT_PX, size * 0.16f); + mProgressTV.setPadding(0, (int) (size * 0.33), 0, 0); mProgressTV.setVisibility(View.VISIBLE); - mLabelTV.setTextSize(TypedValue.COMPLEX_UNIT_PX, w * 0.12f); + mLabelTV.setTextSize(TypedValue.COMPLEX_UNIT_PX, size * 0.12f); mLabelTV.setVisibility(View.VISIBLE); } -- cgit v1.2.3