Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce1c8903 authored by Bruno Martins's avatar Bruno Martins
Browse files

ChargingSoundsSettings: Migrate to AOSP settings

There's no need to have our own settings for this when we can simply
override the existing AOSP ones.

Change-Id: If40843d01b5e3abc461c3fac55483f498b4451fd
parent 147354c1
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2016 The CyanogenMod Project
                   2017 The LineageOS Project
                   2017-2018,2021 The LineageOS Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -26,13 +26,14 @@
        android:summary="@string/charging_sounds_enabled_summary"
        android:defaultValue="true" />

    <lineageos.preference.LineageGlobalSettingSwitchPreference
        android:key="power_notifications_vibrate"
    <lineageos.preference.SecureSettingSwitchPreference
        android:key="charging_vibration_enabled"
        android:title="@string/power_notifications_vibrate_title"
        android:dependency="charging_sounds_enabled" />
        android:dependency="charging_sounds_enabled"
        android:defaultValue="true" />

    <Preference
        android:key="charging_sounds_ringtone"
        android:key="charging_sounds"
        android:title="@string/charging_sounds_ringtone_title"
        android:dependency="charging_sounds_enabled"
        android:persistent="false" />
+27 −29
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *               2017,2019-2020 The LineageOS Project
 *               2017,2019-2021 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -32,28 +32,26 @@ import android.view.View;

import androidx.preference.Preference;

import lineageos.providers.LineageSettings;

import org.lineageos.lineageparts.R;
import org.lineageos.lineageparts.SettingsPreferenceFragment;

public class ChargingSoundsSettings extends SettingsPreferenceFragment {

    private static final String KEY_POWER_NOTIFICATIONS_VIBRATE = "power_notifications_vibrate";
    private static final String KEY_CHARGING_SOUNDS_RINGTONE = "charging_sounds_ringtone";
    private static final String KEY_CHARGING_SOUNDS = "charging_sounds";
    private static final String KEY_CHARGING_VIBRATION_ENABLED = "charging_vibration_enabled";

    // Used for power notification uri string if set to silent
    private static final String RINGTONE_SILENT_URI_STRING = "silent";

    private static final String DEFAULT_POWER_SOUND =
    private static final String DEFAULT_CHARGING_SOUND =
            "/product/media/audio/ui/ChargingStarted.ogg";

    // Request code for charging notification ringtone picker
    private static final int REQUEST_CODE_CHARGING_NOTIFICATIONS_RINGTONE = 1;
    private static final int REQUEST_CODE_CHARGING_SOUND = 1;

    private Preference mChargingSoundsRingtone;
    private Preference mChargingSounds;

    private Uri mDefaultPowerSoundUri;
    private Uri mDefaultChargingSoundUri;

    @Override
    public void onCreate(Bundle savedInstanceState) {
@@ -63,24 +61,24 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {

        Vibrator vibrator = getActivity().getSystemService(Vibrator.class);
        if (vibrator == null || !vibrator.hasVibrator()) {
            removePreference(KEY_POWER_NOTIFICATIONS_VIBRATE);
            removePreference(KEY_CHARGING_VIBRATION_ENABLED);
        }

        mChargingSoundsRingtone = findPreference(KEY_CHARGING_SOUNDS_RINGTONE);
        mChargingSounds = findPreference(KEY_CHARGING_SOUNDS);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        String curTone = LineageSettings.Global.getString(getContentResolver(),
                LineageSettings.Global.POWER_NOTIFICATIONS_RINGTONE);
        String curTone = Settings.Global.getString(getContentResolver(),
                Settings.Global.CHARGING_STARTED_SOUND);

        // Convert default sound file path to a media uri so that we can
        // set a proper default for the ringtone picker.
        mDefaultPowerSoundUri = audioFileToUri(getContext(), DEFAULT_POWER_SOUND);
        mDefaultChargingSoundUri = audioFileToUri(getContext(), DEFAULT_CHARGING_SOUND);

        updateChargingRingtone(curTone);
        updateChargingSounds(curTone);
    }

    private Uri audioFileToUri(Context context, String audioFile) {
@@ -102,12 +100,12 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {
                Integer.toString(id));
    }

    private void updateChargingRingtone(String toneUriString) {
    private void updateChargingSounds(String toneUriString) {
        final String toneTitle;

        if ((toneUriString == null || toneUriString.equals(DEFAULT_POWER_SOUND))
                && mDefaultPowerSoundUri != null) {
            toneUriString = mDefaultPowerSoundUri.toString();
        if ((toneUriString == null || toneUriString.equals(DEFAULT_CHARGING_SOUND))
                && mDefaultChargingSoundUri != null) {
            toneUriString = mDefaultChargingSoundUri.toString();
        }

        if (toneUriString != null && !toneUriString.equals(RINGTONE_SILENT_URI_STRING)) {
@@ -127,17 +125,17 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {
            toneUriString = RINGTONE_SILENT_URI_STRING;
        }

        mChargingSoundsRingtone.setSummary(toneTitle);
        LineageSettings.Global.putString(getContentResolver(),
                LineageSettings.Global.POWER_NOTIFICATIONS_RINGTONE, toneUriString);
        mChargingSounds.setSummary(toneTitle);
        Settings.Global.putString(getContentResolver(),
                Settings.Global.CHARGING_STARTED_SOUND, toneUriString);
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference == mChargingSoundsRingtone) {
            launchNotificationSoundPicker(REQUEST_CODE_CHARGING_NOTIFICATIONS_RINGTONE,
                    LineageSettings.Global.getString(getContentResolver(),
                    LineageSettings.Global.POWER_NOTIFICATIONS_RINGTONE));
        if (preference == mChargingSounds) {
            launchNotificationSoundPicker(REQUEST_CODE_CHARGING_SOUND,
                    Settings.Global.getString(getContentResolver(),
                            Settings.Global.CHARGING_STARTED_SOUND));
        }
        return super.onPreferenceTreeClick(preference);
    }
@@ -149,7 +147,7 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {
                getString(R.string.charging_sounds_ringtone_title));
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, mDefaultPowerSoundUri);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, mDefaultChargingSoundUri);
        if (toneUriString != null && !toneUriString.equals(RINGTONE_SILENT_URI_STRING)) {
            Uri uri = Uri.parse(toneUriString);
            if (uri != null) {
@@ -163,10 +161,10 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_CHARGING_NOTIFICATIONS_RINGTONE
        if (requestCode == REQUEST_CODE_CHARGING_SOUND
                && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            updateChargingRingtone(uri != null ? uri.toString() : null);
            updateChargingSounds(uri != null ? uri.toString() : null);
        }
    }
}