/* * Copyright (C) 2020 The CherishOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.cherish.settings.fragments; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.Intent; import android.os.Bundle; import android.os.UserHandle; import android.provider.Settings; import android.text.Spannable; import android.text.TextUtils; import android.widget.EditText; import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceScreen; import androidx.preference.Preference.OnPreferenceChangeListener; import androidx.preference.SwitchPreference; import com.android.internal.logging.nano.MetricsProto; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; public class CarrierLabel extends SettingsPreferenceFragment implements Preference.OnPreferenceChangeListener { public static final String TAG = "CarrierLabel"; private static final String CUSTOM_CARRIER_LABEL = "custom_carrier_label"; private Preference mCarrierLabel; private String mCarrierLabelText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ContentResolver resolver = getActivity().getContentResolver(); addPreferencesFromResource(R.xml.carrier_label); PreferenceScreen prefSet = getPreferenceScreen(); mCarrierLabel = (Preference) findPreference(CUSTOM_CARRIER_LABEL); updateCustomLabelTextSummary(); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { return false; } public boolean onPreferenceTreeClick(Preference preference) { ContentResolver resolver = getActivity().getContentResolver(); boolean value; if (preference == mCarrierLabel) { AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); alert.setTitle(R.string.custom_carrier_label_title); alert.setMessage(R.string.custom_carrier_label_explain); // Set an EditText view to get user input final EditText input = new EditText(getActivity()); input.setText(TextUtils.isEmpty(mCarrierLabelText) ? "" : mCarrierLabelText); input.setSelection(input.getText().length()); alert.setView(input); alert.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = ((Spannable) input.getText()).toString().trim(); Settings.System.putString(resolver, Settings.System.CUSTOM_CARRIER_LABEL, value); updateCustomLabelTextSummary(); Intent i = new Intent(); i.setAction(Intent.ACTION_CUSTOM_CARRIER_LABEL_CHANGED); getActivity().sendBroadcast(i); } }); alert.setNegativeButton(getString(android.R.string.cancel), null); alert.show(); return true; } return false; } private void updateCustomLabelTextSummary() { mCarrierLabelText = Settings.System.getString( getContentResolver(), Settings.System.CUSTOM_CARRIER_LABEL); if (TextUtils.isEmpty(mCarrierLabelText)) { mCarrierLabel.setSummary(R.string.custom_carrier_label_notset); } else { mCarrierLabel.setSummary(mCarrierLabelText); } } @Override public int getMetricsCategory() { return MetricsProto.MetricsEvent.CHERISH_SETTINGS; } }