File indexing completed on 2024-12-22 04:41:39
0001 /* 0002 * SPDX-FileCopyrightText: 2018 Erik Duisters <e.duisters1@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.FindMyPhonePlugin; 0008 0009 import android.app.Activity; 0010 import android.content.Intent; 0011 import android.content.SharedPreferences; 0012 import android.media.RingtoneManager; 0013 import android.net.Uri; 0014 import android.os.Bundle; 0015 import android.provider.Settings; 0016 0017 import androidx.annotation.NonNull; 0018 import androidx.preference.Preference; 0019 import androidx.preference.PreferenceManager; 0020 0021 import org.kde.kdeconnect.UserInterface.PluginSettingsFragment; 0022 import org.kde.kdeconnect_tp.R; 0023 0024 public class FindMyPhoneSettingsFragment extends PluginSettingsFragment { 0025 private static final int REQUEST_CODE_SELECT_RINGTONE = 1000; 0026 0027 private String preferenceKeyRingtone; 0028 private SharedPreferences sharedPreferences; 0029 private Preference ringtonePreference; 0030 0031 public static FindMyPhoneSettingsFragment newInstance(@NonNull String pluginKey, int layout) { 0032 FindMyPhoneSettingsFragment fragment = new FindMyPhoneSettingsFragment(); 0033 fragment.setArguments(pluginKey, layout); 0034 0035 return fragment; 0036 } 0037 0038 @Override 0039 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 0040 super.onCreatePreferences(savedInstanceState, rootKey); 0041 0042 preferenceKeyRingtone = getString(R.string.findmyphone_preference_key_ringtone); 0043 sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()); 0044 0045 ringtonePreference = getPreferenceScreen().findPreference(preferenceKeyRingtone); 0046 0047 setRingtoneSummary(); 0048 } 0049 0050 private void setRingtoneSummary() { 0051 String ringtone = sharedPreferences.getString(preferenceKeyRingtone, Settings.System.DEFAULT_RINGTONE_URI.toString()); 0052 0053 Uri ringtoneUri = Uri.parse(ringtone); 0054 0055 ringtonePreference.setSummary(RingtoneManager.getRingtone(requireContext(), ringtoneUri).getTitle(requireContext())); 0056 } 0057 0058 @Override 0059 public boolean onPreferenceTreeClick(Preference preference) { 0060 /* 0061 * There is no RingtonePreference in support library nor androidx, this is the workaround proposed here: 0062 * https://issuetracker.google.com/issues/37057453 0063 */ 0064 0065 if (preference.hasKey() && preference.getKey().equals(preferenceKeyRingtone)) { 0066 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); 0067 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); 0068 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); 0069 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false); 0070 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI); 0071 0072 String existingValue = sharedPreferences.getString(preferenceKeyRingtone, null); 0073 if (existingValue != null) { 0074 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(existingValue)); 0075 } else { 0076 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Settings.System.DEFAULT_RINGTONE_URI); 0077 } 0078 0079 startActivityForResult(intent, REQUEST_CODE_SELECT_RINGTONE); 0080 return true; 0081 } 0082 return super.onPreferenceTreeClick(preference); 0083 } 0084 0085 @Override 0086 public void onActivityResult(int requestCode, int resultCode, Intent data) { 0087 if (requestCode == REQUEST_CODE_SELECT_RINGTONE && resultCode == Activity.RESULT_OK) { 0088 Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 0089 0090 if (uri != null) { 0091 sharedPreferences.edit() 0092 .putString(preferenceKeyRingtone, uri.toString()) 0093 .apply(); 0094 0095 setRingtoneSummary(); 0096 } 0097 } 0098 } 0099 }