sdm660-common: DeviceSettings: Implement Clear Speaker

Co-authored-by: Adithya R <radithya2002@gmail.com>
Co-authored-by: AshutoshSundresh <ashutoshsundresh@gmail.com>

Conflicts:
	DeviceSettings/src/org/lineageos/settings/device/DeviceSettings.java

Conflicts:
	DeviceSettings/res/xml/preferences_xiaomi_parts.xml
Signed-off-by: pix106 <sbordenave@gmail.com>
This commit is contained in:
Omar Hamad 2021-03-05 17:23:12 +02:00 committed by pix106
parent ff88467668
commit e283eaa87c
9 changed files with 246 additions and 0 deletions

View file

@ -80,6 +80,17 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".speaker.ClearSpeakerActivity"
android:exported="false"
android:label="@string/clear_speaker_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service <service
android:name=".FPSInfoService" android:name=".FPSInfoService"
android:exported="true"> android:exported="true">

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:tint="?android:attr/colorControlNormal"
android:viewportHeight="24">
<path
android:pathData="M0 0h24v24H0z" />
<path
android:fillColor="#FFFFFFFF"
android:pathData="M20 4H4c-1.1 0-2 0.9-2 2v12c0 1.1 0.9 2 2 2h16c1.1 0 2-0.9 2-2V6c0-1.1-0.9-2-2-2zM7.76 16.24l-1.41 1.41C4.78 16.1 4 14.05 4 12c0-2.05 0.78 -4.1 2.34-5.66l1.41 1.41C6.59 8.93 6 10.46 6 12s0.59 3.07 1.76 4.24zM12 16c-2.21 0-4-1.79-4-4s1.79-4 4-4 4 1.79 4 4-1.79 4-4 4zm5.66 1.66l-1.41-1.41C17.41 15.07 18 13.54 18 12s-0.59-3.07-1.76-4.24l1.41-1.41C19.22 7.9 20 9.95 20 12c0 2.05-0.78 4.1-2.34 5.66zM12 10c-1.1 0-2 0.9-2 2s0.9 2 2 2 2-0.9 2-2-0.9-2-2-2z" />
</vector>

Binary file not shown.

View file

@ -55,6 +55,14 @@
<string name="hue">Hue</string> <string name="hue">Hue</string>
<string name="grayscale">Grayscale Display</string> <string name="grayscale">Grayscale Display</string>
<!-- Speaker -->
<string name="speaker_category">Speaker</string>
<!-- Clear Speaker -->
<string name="clear_speaker_title">Clear speaker</string>
<string name="clear_speaker_summary">Play a 30-second audio to clear the speaker</string>
<string name="clear_speaker_description">Run this feature once or twice if you find that your speaker is lightly blocked by dust. Set media volume to maximum.\n\nIf the speaker is blocked heavily, run this feature 2-5 times while shaking your device with the speaker facing downwards.</string>
<string name="presets_dialog_title">Select Preset</string> <string name="presets_dialog_title">Select Preset</string>
<!-- Presets --> <!-- Presets -->
<string name="preset_default">Default</string> <string name="preset_default">Default</string>

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/clear_speaker_title">
<SwitchPreference
android:key="clear_speaker_pref"
android:title="@string/clear_speaker_title"
android:icon="@drawable/ic_speaker_cleaner_icon"
android:summary="@string/clear_speaker_summary"/>
<com.android.settingslib.widget.FooterPreference
android:key="footer_preference"
android:title="@string/clear_speaker_description"
android:selectable="false" />
</PreferenceScreen>

View file

@ -159,4 +159,20 @@
android:title="@string/hall_title" android:title="@string/hall_title"
android:icon="@drawable/ic_flip" /> android:icon="@drawable/ic_flip" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory
android:key="speaker"
android:title="@string/speaker_category">
<Preference
android:key="clear_speaker_settings"
android:title="@string/clear_speaker_title"
android:icon="@drawable/ic_speaker_cleaner_icon" >
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.xiaomi.parts.speaker.ClearSpeakerActivity"
android:targetPackage="com.xiaomi.parts.speaker" />
</Preference>
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View file

@ -24,6 +24,7 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import org.lineageos.settings.device.kcal.KCalSettingsActivity; import org.lineageos.settings.device.kcal.KCalSettingsActivity;
import org.lineageos.settings.device.speaker.ClearSpeakerActivity;
import org.lineageos.settings.device.preferences.SecureSettingListPreference; import org.lineageos.settings.device.preferences.SecureSettingListPreference;
import org.lineageos.settings.device.preferences.SecureSettingSwitchPreference; import org.lineageos.settings.device.preferences.SecureSettingSwitchPreference;
import org.lineageos.settings.device.preferences.VibrationSeekBarPreference; import org.lineageos.settings.device.preferences.VibrationSeekBarPreference;
@ -79,6 +80,9 @@ public class DeviceSettings extends PreferenceFragment implements
private static final String DEVICE_JASON_PACKAGE_NAME = "org.lineageos.settings.devicex"; private static final String DEVICE_JASON_PACKAGE_NAME = "org.lineageos.settings.devicex";
private static final String PREF_DEVICE_JASON = "device_jason"; private static final String PREF_DEVICE_JASON = "device_jason";
private static final String PREF_CLEAR_SPEAKER = "clear_speaker_settings";
private Preference mClearSpeakerPref;
private SecureSettingSwitchPreference mEnableDirac; private SecureSettingSwitchPreference mEnableDirac;
private SecureSettingListPreference mHeadsetType; private SecureSettingListPreference mHeadsetType;
private SecureSettingListPreference mPreset; private SecureSettingListPreference mPreset;
@ -87,6 +91,14 @@ public class DeviceSettings extends PreferenceFragment implements
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences_xiaomi_parts, rootKey); setPreferencesFromResource(R.xml.preferences_xiaomi_parts, rootKey);
mClearSpeakerPref = (Preference) findPreference(PREF_CLEAR_SPEAKER);
mClearSpeakerPref.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(getActivity().getApplicationContext(), ClearSpeakerActivity.class);
startActivity(intent);
return true;
});
// LED Brightness // LED Brightness
if (FileUtils.fileWritable(NOTIF_LED_PATH)) { if (FileUtils.fileWritable(NOTIF_LED_PATH)) {
NotificationLedSeekBarPreference notifLedBrightness = NotificationLedSeekBarPreference notifLedBrightness =

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 2020 Paranoid Android
*
* 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 org.lineageos.settings.device.speaker;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.MenuItem;
public class ClearSpeakerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
Fragment fragment = getFragmentManager().findFragmentById(android.R.id.content);
ClearSpeakerFragment clearSpeakerFragment;
if (fragment == null) {
clearSpeakerFragment = new ClearSpeakerFragment();
getFragmentManager().beginTransaction()
.add(android.R.id.content, clearSpeakerFragment)
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}

View file

@ -0,0 +1,118 @@
/*
* Copyright (C) 2020 Paranoid Android
*
* 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 org.lineageos.settings.device.speaker;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragment;
import androidx.preference.SwitchPreference;
import org.lineageos.settings.device.R;
import java.io.IOException;
public class ClearSpeakerFragment extends PreferenceFragment implements
Preference.OnPreferenceChangeListener {
private static final String TAG = ClearSpeakerFragment.class.getSimpleName();
private static final String PREF_CLEAR_SPEAKER = "clear_speaker_pref";
private AudioManager mAudioManager;
private Handler mHandler;
private MediaPlayer mMediaPlayer;
private SwitchPreference mClearSpeakerPref;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.clear_speaker_settings);
mClearSpeakerPref = (SwitchPreference) findPreference(PREF_CLEAR_SPEAKER);
mClearSpeakerPref.setOnPreferenceChangeListener(this);
mHandler = new Handler();
mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mClearSpeakerPref) {
boolean value = (Boolean) newValue;
if (value) {
if (startPlaying()) {
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(() -> {
stopPlaying();
}, 30000);
return true;
}
}
}
return false;
}
@Override
public void onStop() {
stopPlaying();
super.onStop();
}
public boolean startPlaying() {
mAudioManager.setParameters("status_earpiece_clean=on");
mMediaPlayer = new MediaPlayer();
getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
try {
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.clear_speaker_sound);
try {
mMediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mClearSpeakerPref.setEnabled(false);
mMediaPlayer.setVolume(1.0f, 1.0f);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException ioe) {
Log.e(TAG, "Failed to play speaker clean sound!", ioe);
return false;
}
return true;
}
public void stopPlaying() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.reset();
mMediaPlayer.release();
}
mAudioManager.setParameters("status_earpiece_clean=off");
mClearSpeakerPref.setEnabled(true);
mClearSpeakerPref.setChecked(false);
}
}