mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Convert RingtoneManager to Kotlin
This commit is contained in:
@@ -52,6 +52,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
|
||||
private SharedPreferences sharedPrefs;
|
||||
|
||||
private RingtoneManager ringtoneManager;
|
||||
|
||||
@Nullable
|
||||
private Preferences prefs;
|
||||
|
||||
@@ -60,7 +62,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
{
|
||||
if (requestCode == RINGTONE_REQUEST_CODE)
|
||||
{
|
||||
RingtoneManager.parseRingtoneData(getContext(), data);
|
||||
ringtoneManager.update(data);
|
||||
updateRingtoneDescription();
|
||||
return;
|
||||
}
|
||||
@@ -86,9 +88,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
setResultOnPreferenceClick("exportDB", RESULT_EXPORT_DB);
|
||||
setResultOnPreferenceClick("repairDB", RESULT_REPAIR_DB);
|
||||
setResultOnPreferenceClick("bugReport", RESULT_BUG_REPORT);
|
||||
|
||||
updateRingtoneDescription();
|
||||
updateSync();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,6 +122,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
public void onResume()
|
||||
{
|
||||
super.onResume();
|
||||
this.ringtoneManager = new RingtoneManager(getActivity());
|
||||
|
||||
sharedPrefs = getPreferenceManager().getSharedPreferences();
|
||||
sharedPrefs.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
@@ -133,6 +134,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
devCategory.removeAll();
|
||||
devCategory.setVisible(false);
|
||||
}
|
||||
|
||||
updateRingtoneDescription();
|
||||
updateSync();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -156,7 +160,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
|
||||
private void showRingtonePicker()
|
||||
{
|
||||
Uri existingRingtoneUri = RingtoneManager.getRingtoneUri(getContext());
|
||||
Uri existingRingtoneUri = ringtoneManager.getURI();
|
||||
Uri defaultRingtoneUri = Settings.System.DEFAULT_NOTIFICATION_URI;
|
||||
|
||||
Intent intent = new Intent(ACTION_RINGTONE_PICKER);
|
||||
@@ -170,7 +174,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
|
||||
private void updateRingtoneDescription()
|
||||
{
|
||||
String ringtoneName = RingtoneManager.getRingtoneName(getContext());
|
||||
String ringtoneName = ringtoneManager.getName();
|
||||
if (ringtoneName == null) return;
|
||||
Preference ringtonePreference = findPreference("reminderSound");
|
||||
ringtonePreference.setSummary(ringtoneName);
|
||||
|
||||
@@ -31,15 +31,15 @@ import org.isoron.uhabits.core.models.*
|
||||
import org.isoron.uhabits.core.preferences.*
|
||||
import org.isoron.uhabits.core.ui.*
|
||||
import org.isoron.uhabits.intents.*
|
||||
import org.isoron.uhabits.notifications.RingtoneManager.*
|
||||
import javax.inject.*
|
||||
|
||||
@AppScope
|
||||
class AndroidNotificationTray
|
||||
@Inject constructor(
|
||||
@param:AppContext private val context: Context,
|
||||
@AppContext private val context: Context,
|
||||
private val pendingIntents: PendingIntentFactory,
|
||||
private val preferences: Preferences
|
||||
private val preferences: Preferences,
|
||||
private val ringtoneManager: RingtoneManager
|
||||
) : NotificationTray.SystemTray {
|
||||
|
||||
override fun removeNotification(id: Int) {
|
||||
@@ -79,7 +79,7 @@ class AndroidNotificationTray
|
||||
.setDeleteIntent(pendingIntents.dismissNotification(habit))
|
||||
.addAction(checkAction)
|
||||
.addAction(snoozeAction)
|
||||
.setSound(getRingtoneUri(context))
|
||||
.setSound(ringtoneManager.getURI())
|
||||
.extend(wearableExtender)
|
||||
.setWhen(reminderTime)
|
||||
.setShowWhen(true)
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.notifications;
|
||||
|
||||
import android.content.*;
|
||||
import android.media.*;
|
||||
import android.net.*;
|
||||
import android.preference.*;
|
||||
import android.provider.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
import static android.media.RingtoneManager.EXTRA_RINGTONE_PICKED_URI;
|
||||
import static android.media.RingtoneManager.getRingtone;
|
||||
|
||||
@AppScope
|
||||
public class RingtoneManager
|
||||
{
|
||||
@Inject
|
||||
public RingtoneManager()
|
||||
{
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getRingtoneName(Context context)
|
||||
{
|
||||
try
|
||||
{
|
||||
Uri ringtoneUri = getRingtoneUri(context);
|
||||
String ringtoneName =
|
||||
context.getResources().getString(R.string.none);
|
||||
|
||||
if (ringtoneUri != null)
|
||||
{
|
||||
Ringtone ringtone = getRingtone(context, ringtoneUri);
|
||||
if (ringtone != null)
|
||||
{
|
||||
ringtoneName = ringtone.getTitle(context);
|
||||
ringtone.stop();
|
||||
}
|
||||
}
|
||||
|
||||
return ringtoneName;
|
||||
}
|
||||
catch (RuntimeException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Uri getRingtoneUri(Context context)
|
||||
{
|
||||
Uri ringtoneUri = null;
|
||||
Uri defaultRingtoneUri = Settings.System.DEFAULT_NOTIFICATION_URI;
|
||||
|
||||
SharedPreferences prefs =
|
||||
PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String prefRingtoneUri =
|
||||
prefs.getString("pref_ringtone_uri", defaultRingtoneUri.toString());
|
||||
if (prefRingtoneUri.length() > 0)
|
||||
ringtoneUri = Uri.parse(prefRingtoneUri);
|
||||
|
||||
return ringtoneUri;
|
||||
}
|
||||
|
||||
public static void parseRingtoneData(Context context, @Nullable Intent data)
|
||||
{
|
||||
if (data == null) return;
|
||||
|
||||
Uri ringtoneUri = data.getParcelableExtra(EXTRA_RINGTONE_PICKED_URI);
|
||||
|
||||
if (ringtoneUri != null)
|
||||
{
|
||||
SharedPreferences prefs =
|
||||
PreferenceManager.getDefaultSharedPreferences(context);
|
||||
prefs
|
||||
.edit()
|
||||
.putString("pref_ringtone_uri", ringtoneUri.toString())
|
||||
.apply();
|
||||
}
|
||||
else
|
||||
{
|
||||
SharedPreferences prefs =
|
||||
PreferenceManager.getDefaultSharedPreferences(context);
|
||||
prefs.edit().putString("pref_ringtone_uri", "").apply();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.notifications
|
||||
|
||||
import android.content.*
|
||||
import android.media.RingtoneManager.*
|
||||
import android.net.*
|
||||
import android.preference.*
|
||||
import android.provider.*
|
||||
import org.isoron.androidbase.*
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.core.*
|
||||
import javax.inject.*
|
||||
|
||||
@AppScope
|
||||
class RingtoneManager
|
||||
@Inject constructor(@AppContext private val context: Context) {
|
||||
|
||||
val prefs: SharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
|
||||
fun getName(): String? {
|
||||
try {
|
||||
var ringtoneName = context.resources.getString(R.string.none)
|
||||
val ringtoneUri = getURI()
|
||||
if (ringtoneUri != null) {
|
||||
val ringtone = getRingtone(context, ringtoneUri)
|
||||
if (ringtone != null) ringtoneName = ringtone.getTitle(context)
|
||||
}
|
||||
return ringtoneName
|
||||
} catch (e: RuntimeException) {
|
||||
e.printStackTrace()
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun getURI(): Uri? {
|
||||
var ringtoneUri: Uri? = null
|
||||
val defaultRingtoneUri = Settings.System.DEFAULT_NOTIFICATION_URI
|
||||
val prefRingtoneUri = prefs.getString("pref_ringtone_uri",
|
||||
defaultRingtoneUri.toString())
|
||||
if (prefRingtoneUri.isNotEmpty())
|
||||
ringtoneUri = Uri.parse(prefRingtoneUri)
|
||||
|
||||
return ringtoneUri
|
||||
}
|
||||
|
||||
fun update(data: Intent?) {
|
||||
if (data == null) return
|
||||
val ringtoneUri = data.getParcelableExtra<Uri>(EXTRA_RINGTONE_PICKED_URI)
|
||||
if (ringtoneUri != null) {
|
||||
prefs.edit().putString("pref_ringtone_uri", ringtoneUri.toString()).apply()
|
||||
} else {
|
||||
prefs.edit().putString("pref_ringtone_uri", "").apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user