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

Commit aeeabd21 authored by Sam Mortimer's avatar Sam Mortimer Committed by Bruno Martins
Browse files

Parts: Convert charging sound path to uri

Change-Id: I802c55167adabb664b9bd83c0da8b037e869c6e6
parent 89215be1
Loading
Loading
Loading
Loading
+54 −19
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *               2017 The LineageOS Project
 *               2017,2019-2020 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.
@@ -18,6 +18,7 @@
package org.lineageos.lineageparts.sounds;

import android.app.Activity;
import android.database.Cursor;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
@@ -25,7 +26,9 @@ import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.provider.Settings;
import android.view.View;

import androidx.preference.Preference;

@@ -42,11 +45,16 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {
    // 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 =
            "/system/product/media/audio/ui/WirelessChargingStarted.ogg";

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

    private Preference mChargingSoundsRingtone;

    private Uri mDefaultPowerSoundUri;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -59,42 +67,70 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {
        }

        mChargingSoundsRingtone = findPreference(KEY_CHARGING_SOUNDS_RINGTONE);
    }

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

        String curTone = LineageSettings.Global.getString(getContentResolver(),
                LineageSettings.Global.POWER_NOTIFICATIONS_RINGTONE);
        if (curTone == null) {
            updateChargingRingtone(Settings.System.DEFAULT_NOTIFICATION_URI.toString(), true);
        } else {
            updateChargingRingtone(curTone, false);

        // 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);

        updateChargingRingtone(curTone);
    }

    private Uri audioFileToUri(Context context, String audioFile) {
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Media._ID },
                MediaStore.Audio.Media.DATA + "=? ",
                new String[] { audioFile }, null);
        if (cursor == null) {
            return null;
        }
        if (!cursor.moveToFirst()) {
            cursor.close();
            return null;
        }
        int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
        cursor.close();
        return Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
                Integer.toString(id));
    }

    private void updateChargingRingtone(String toneUriString, boolean persist) {
        final String toneName;
    private void updateChargingRingtone(String toneUriString) {
        final String toneTitle;

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

        if (toneUriString != null && !toneUriString.equals(RINGTONE_SILENT_URI_STRING)) {
            final Ringtone ringtone = RingtoneManager.getRingtone(getActivity(),
                    Uri.parse(toneUriString));
            if (ringtone != null) {
                toneName = ringtone.getTitle(getActivity());
                toneTitle = ringtone.getTitle(getActivity());
            } else {
                // Unlikely to ever happen, but is possible if the ringtone
                // previously chosen is removed during an upgrade
                toneName = "";
                toneTitle = "";
                toneUriString = Settings.System.DEFAULT_NOTIFICATION_URI.toString();
                persist = true;
            }
        } else {
            // Silent
            toneName = getString(R.string.charging_sounds_ringtone_silent);
            toneTitle = getString(R.string.charging_sounds_ringtone_silent);
            toneUriString = RINGTONE_SILENT_URI_STRING;
        }

        mChargingSoundsRingtone.setSummary(toneName);
        if (persist) {
        mChargingSoundsRingtone.setSummary(toneTitle);
        LineageSettings.Global.putString(getContentResolver(),
                LineageSettings.Global.POWER_NOTIFICATIONS_RINGTONE, toneUriString);
    }
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
@@ -113,8 +149,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,
                Settings.System.DEFAULT_NOTIFICATION_URI);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, mDefaultPowerSoundUri);
        if (toneUriString != null && !toneUriString.equals(RINGTONE_SILENT_URI_STRING)) {
            Uri uri = Uri.parse(toneUriString);
            if (uri != null) {
@@ -131,7 +166,7 @@ public class ChargingSoundsSettings extends SettingsPreferenceFragment {
        if (requestCode == REQUEST_CODE_CHARGING_NOTIFICATIONS_RINGTONE
                && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            updateChargingRingtone(uri != null ? uri.toString() : null, true);
            updateChargingRingtone(uri != null ? uri.toString() : null);
        }
    }
}