1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.gestures; 18 19 import static android.provider.Settings.Secure.VOLUME_HUSH_GESTURE; 20 import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE; 21 import static android.provider.Settings.Secure.VOLUME_HUSH_OFF; 22 import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE; 23 24 import android.content.Context; 25 import android.os.Bundle; 26 import android.provider.Settings; 27 import android.support.annotation.VisibleForTesting; 28 import android.support.v7.preference.ListPreference; 29 import android.support.v7.preference.Preference; 30 import android.support.v7.preference.PreferenceScreen; 31 32 import com.android.settings.R; 33 import com.android.settings.core.BasePreferenceController; 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settings.widget.VideoPreference; 36 import com.android.settingslib.core.lifecycle.LifecycleObserver; 37 import com.android.settingslib.core.lifecycle.events.OnCreate; 38 import com.android.settingslib.core.lifecycle.events.OnPause; 39 import com.android.settingslib.core.lifecycle.events.OnResume; 40 import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState; 41 42 public class PreventRingingPreferenceController extends BasePreferenceController 43 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, 44 LifecycleObserver, OnResume, OnPause, OnCreate, OnSaveInstanceState { 45 46 private static final String PREF_KEY_VIDEO = "gesture_prevent_ringing_video"; 47 @VisibleForTesting 48 static final String KEY_VIDEO_PAUSED = "key_video_paused"; 49 50 private VideoPreference mVideoPreference; 51 @VisibleForTesting 52 boolean mVideoPaused; 53 54 private final String SECURE_KEY = VOLUME_HUSH_GESTURE; 55 PreventRingingPreferenceController(Context context, String key)56 public PreventRingingPreferenceController(Context context, String key) { 57 super(context, key); 58 } 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 return mContext.getResources().getBoolean( 63 com.android.internal.R.bool.config_volumeHushGestureEnabled) 64 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 65 } 66 67 @Override displayPreference(PreferenceScreen screen)68 public void displayPreference(PreferenceScreen screen) { 69 super.displayPreference(screen); 70 if (isAvailable()) { 71 mVideoPreference = (VideoPreference) screen.findPreference(getVideoPrefKey()); 72 } 73 } 74 75 @Override updateState(Preference preference)76 public void updateState(Preference preference) { 77 super.updateState(preference); 78 if (preference != null) { 79 if (preference instanceof ListPreference) { 80 ListPreference pref = (ListPreference) preference; 81 int value = Settings.Secure.getInt( 82 mContext.getContentResolver(), SECURE_KEY, VOLUME_HUSH_VIBRATE); 83 switch (value) { 84 case VOLUME_HUSH_VIBRATE: 85 pref.setValue(String.valueOf(value)); 86 break; 87 case VOLUME_HUSH_MUTE: 88 pref.setValue(String.valueOf(value)); 89 break; 90 default: 91 pref.setValue(String.valueOf(VOLUME_HUSH_OFF)); 92 } 93 } 94 } 95 } 96 97 @Override getSummary()98 public CharSequence getSummary() { 99 int value = Settings.Secure.getInt( 100 mContext.getContentResolver(), SECURE_KEY, VOLUME_HUSH_VIBRATE); 101 int summary; 102 switch (value) { 103 case VOLUME_HUSH_VIBRATE: 104 summary = R.string.prevent_ringing_option_vibrate_summary; 105 break; 106 case VOLUME_HUSH_MUTE: 107 summary = R.string.prevent_ringing_option_mute_summary; 108 break; 109 default: 110 summary = R.string.prevent_ringing_option_none_summary; 111 } 112 return mContext.getString(summary); 113 } 114 115 @Override onCreate(Bundle savedInstanceState)116 public void onCreate(Bundle savedInstanceState) { 117 if (savedInstanceState != null) { 118 mVideoPaused = savedInstanceState.getBoolean(KEY_VIDEO_PAUSED, false); 119 } 120 } 121 122 @Override onSaveInstanceState(Bundle outState)123 public void onSaveInstanceState(Bundle outState) { 124 outState.putBoolean(KEY_VIDEO_PAUSED, mVideoPaused); 125 } 126 127 @Override onPause()128 public void onPause() { 129 if (mVideoPreference != null) { 130 mVideoPaused = mVideoPreference.isVideoPaused(); 131 mVideoPreference.onViewInvisible(); 132 } 133 } 134 135 @Override onResume()136 public void onResume() { 137 if (mVideoPreference != null) { 138 mVideoPreference.onViewVisible(mVideoPaused); 139 } 140 } 141 getVideoPrefKey()142 protected String getVideoPrefKey() { 143 return PREF_KEY_VIDEO; 144 } 145 146 @Override onPreferenceChange(Preference preference, Object newValue)147 public boolean onPreferenceChange(Preference preference, Object newValue) { 148 int value = Integer.parseInt((String) newValue); 149 Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY, value); 150 preference.setSummary(getSummary()); 151 return true; 152 } 153 } 154