mirror of https://github.com/ThmmyNoLife/mTHMMY
Thodoris1999
7 years ago
20 changed files with 376 additions and 98 deletions
@ -0,0 +1,44 @@ |
|||
package gr.thmmy.mthmmy.activities.settings; |
|||
|
|||
import android.os.Bundle; |
|||
import android.support.v4.app.Fragment; |
|||
import android.support.v4.app.FragmentTransaction; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.base.BaseActivity; |
|||
|
|||
public class SettingsActivity extends BaseActivity { |
|||
public static final String NOTIFICATION_VIBRATION_KEY = "pref_notification_vibration_enable_key"; |
|||
public static final String APP_SIGNATURE_ENABLE_KEY = "pref_posting_app_signature_enable_key"; |
|||
|
|||
@Override |
|||
protected void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
setContentView(R.layout.activity_settings); |
|||
|
|||
//Initialize toolbar
|
|||
toolbar = findViewById(R.id.toolbar); |
|||
toolbar.setTitle("Settings"); |
|||
setSupportActionBar(toolbar); |
|||
if (getSupportActionBar() != null) { |
|||
getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
|||
getSupportActionBar().setDisplayShowHomeEnabled(true); |
|||
} |
|||
|
|||
createDrawer(); |
|||
drawer.setSelection(SETTINGS_ID); |
|||
|
|||
if (savedInstanceState == null) { |
|||
Fragment preferenceFragment = new SettingsFragment(); |
|||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); |
|||
fragmentTransaction.add(R.id.pref_container, preferenceFragment); |
|||
fragmentTransaction.commit(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected void onResume() { |
|||
drawer.setSelection(SETTINGS_ID); |
|||
super.onResume(); |
|||
} |
|||
} |
@ -0,0 +1,82 @@ |
|||
package gr.thmmy.mthmmy.activities.settings; |
|||
|
|||
import android.app.Activity; |
|||
import android.content.Context; |
|||
import android.content.Intent; |
|||
import android.content.SharedPreferences; |
|||
import android.media.RingtoneManager; |
|||
import android.net.Uri; |
|||
import android.os.Bundle; |
|||
import android.provider.Settings; |
|||
import android.support.v7.preference.Preference; |
|||
import android.support.v7.preference.PreferenceFragmentCompat; |
|||
import android.util.Log; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
|
|||
public class SettingsFragment extends PreferenceFragmentCompat { |
|||
private static final int REQUEST_CODE_ALERT_RINGTONE = 2; |
|||
public static final String SETTINGS_SHARED_PREFS = "settingsSharedPrefs"; |
|||
public static final String SELECTED_RINGTONE = "selectedRingtoneKey"; |
|||
private static final String SELECTED_NOTIFICATIONS_SOUND = "pref_notifications_select_sound_key"; |
|||
private static final String SILENT_SELECTED = "STFU"; |
|||
|
|||
private SharedPreferences settingsFile; |
|||
|
|||
@Override |
|||
public void onCreatePreferences(Bundle bundle, String s) { |
|||
// Load the Preferences from the XML file
|
|||
addPreferencesFromResource(R.xml.app_preferences); |
|||
} |
|||
|
|||
@Override |
|||
public boolean onPreferenceTreeClick(Preference preference) { |
|||
if (preference.getKey().equals(SELECTED_NOTIFICATIONS_SOUND)) { |
|||
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); |
|||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); |
|||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); |
|||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); |
|||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI); |
|||
|
|||
Activity activity = this.getActivity(); |
|||
settingsFile = activity != null |
|||
? activity.getSharedPreferences(SETTINGS_SHARED_PREFS, Context.MODE_PRIVATE) |
|||
: null; |
|||
String existingValue = settingsFile != null |
|||
? settingsFile.getString(SELECTED_RINGTONE, null) |
|||
: null; |
|||
if (existingValue != null) { |
|||
if (existingValue.equals(SILENT_SELECTED)) { |
|||
//Selects "Silent"
|
|||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null); |
|||
} else { |
|||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(existingValue)); |
|||
} |
|||
} else { |
|||
//No ringtone has been selected, set to the default
|
|||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Settings.System.DEFAULT_NOTIFICATION_URI); |
|||
} |
|||
|
|||
startActivityForResult(intent, REQUEST_CODE_ALERT_RINGTONE); |
|||
return true; |
|||
} else { |
|||
return super.onPreferenceTreeClick(preference); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
|||
if (requestCode == REQUEST_CODE_ALERT_RINGTONE && data != null) { |
|||
Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); |
|||
SharedPreferences.Editor editor = settingsFile.edit(); |
|||
if (ringtone != null) { |
|||
editor.putString(SELECTED_RINGTONE, ringtone.toString()).apply(); |
|||
} else { |
|||
// "Silent" was selected
|
|||
editor.putString(SELECTED_RINGTONE, SILENT_SELECTED).apply(); |
|||
} |
|||
} else { |
|||
super.onActivityResult(requestCode, resultCode, data); |
|||
} |
|||
} |
|||
} |
After Width: | Height: | Size: 581 B |
After Width: | Height: | Size: 418 B |
After Width: | Height: | Size: 800 B |
After Width: | Height: | Size: 1.2 KiB |
@ -0,0 +1,33 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" |
|||
xmlns:app="http://schemas.android.com/apk/res-auto" |
|||
xmlns:tools="http://schemas.android.com/tools" |
|||
android:id="@+id/main_content" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:fitsSystemWindows="true" |
|||
tools:context=".activities.settings.SettingsActivity"> |
|||
|
|||
<android.support.design.widget.AppBarLayout |
|||
android:id="@+id/appbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:paddingTop="@dimen/appbar_padding_top" |
|||
android:theme="@style/ToolbarTheme"> |
|||
|
|||
<android.support.v7.widget.Toolbar |
|||
android:id="@+id/toolbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="?attr/actionBarSize" |
|||
android:background="?attr/colorPrimary" |
|||
android:gravity="center" |
|||
app:popupTheme="@style/ToolbarTheme" /> |
|||
</android.support.design.widget.AppBarLayout> |
|||
|
|||
<FrameLayout |
|||
android:id="@+id/pref_container" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> |
|||
|
|||
</android.support.design.widget.CoordinatorLayout> |
@ -0,0 +1,35 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> |
|||
|
|||
<android.support.v7.preference.PreferenceCategory android:title="@string/pref_category_notifications"> |
|||
|
|||
<!--<android.support.v7.preference.SwitchPreferenceCompat |
|||
android:defaultValue="true" |
|||
android:key="pref_notifications_enable_key" |
|||
android:summary="@string/pref_summary_notifications_enable" |
|||
android:title="@string/pref_title_notifications_enable" />--> |
|||
|
|||
<android.support.v7.preference.SwitchPreferenceCompat |
|||
android:defaultValue="true" |
|||
android:key="pref_notification_vibration_enable_key" |
|||
android:summary="@string/pref_summary_notification_vibration_enable" |
|||
android:title="@string/pref_title_notification_vibration_enable" /> |
|||
|
|||
<Preference |
|||
android:key="pref_notifications_select_sound_key" |
|||
android:summary="@string/pref_summary_notifications_sound" |
|||
android:title="@string/pref_title_notifications_sound" /> |
|||
|
|||
</android.support.v7.preference.PreferenceCategory> |
|||
|
|||
<android.support.v7.preference.PreferenceCategory android:title="@string/pref_category_posting"> |
|||
|
|||
<android.support.v7.preference.SwitchPreferenceCompat |
|||
android:defaultValue="true" |
|||
android:key="pref_posting_app_signature_enable_key" |
|||
android:summary="@string/pref_summary_posting_app_signature_enable" |
|||
android:title="@string/pref_title_posting_app_signature_enable" /> |
|||
|
|||
</android.support.v7.preference.PreferenceCategory> |
|||
|
|||
</android.support.v7.preference.PreferenceScreen> |
Loading…
Reference in new issue