Advertisement
csphelps76

Untitled

Feb 8th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.47 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2012 CyanogenMod
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package com.android.settings.cyanogenmod;
  18.  
  19. import android.app.AlertDialog;
  20. import android.content.ContentResolver;
  21. import android.content.Context;
  22. import android.content.pm.PackageManager.NameNotFoundException;
  23. import android.os.Bundle;
  24. import android.content.Context;
  25. import android.content.DialogInterface;
  26. import android.content.Intent;
  27. import android.os.RemoteException;
  28. import android.os.ServiceManager;
  29. import android.view.IWindowManager;
  30. import android.preference.CheckBoxPreference;
  31. import android.preference.Preference;
  32. import android.preference.PreferenceScreen;
  33. import android.provider.Settings;
  34. import android.preference.CheckBoxPreference;
  35. import android.preference.ListPreference;
  36. import android.preference.Preference;
  37. import android.preference.PreferenceActivity;
  38. import android.preference.PreferenceGroup;
  39. import android.preference.PreferenceScreen;
  40. import android.preference.Preference.OnPreferenceChangeListener;
  41. import android.provider.Settings;
  42. import android.text.Spannable;
  43. import android.util.Log;
  44. import android.view.IWindowManager;
  45. import android.view.Display;
  46. import android.view.LayoutInflater;
  47. import android.widget.EditText;
  48.  
  49. import com.android.settings.R;
  50. import com.android.settings.SettingsPreferenceFragment;
  51. import com.android.settings.Utils;
  52.  
  53. import java.util.regex.Matcher;
  54. import java.util.regex.Pattern;
  55.  
  56. public class SystemSettings extends SettingsPreferenceFragment implements
  57.     Preference.OnPreferenceChangeListener{
  58.  
  59.     private static final String TAG = "SystemSettings";
  60.  
  61.     private static final String KEY_POWER_BUTTON_TORCH = "power_button_torch";
  62.  
  63.     private CheckBoxPreference mPowerButtonTorch;
  64.     private static final String KEY_CHRONUS = "chronus";
  65.     private static final String PREF_FORCE_DUAL_PANEL = "force_dualpanel";
  66.     private static final String PREF_CUSTOM_CARRIER_LABEL = "custom_carrier_label";
  67.  
  68.     Preference mCustomLabel;
  69.  
  70.     String mCustomLabelText = null;
  71.  
  72.     Context mContext;
  73.  
  74.     CheckBoxPreference mDualpane;
  75.     private boolean torchSupported() {
  76.         return getResources().getBoolean(R.bool.has_led_flash);
  77.     }
  78.  
  79.     @Override
  80.     public void onCreate(Bundle savedInstanceState) {
  81.         super.onCreate(savedInstanceState);
  82.  
  83.         addPreferencesFromResource(R.xml.system_settings);
  84.  
  85.         mCustomLabel = findPreference(PREF_CUSTOM_CARRIER_LABEL);
  86.         updateCustomLabelTextSummary();
  87.  
  88.         // Dont display the lock clock preference if its not installed
  89.         removePreferenceIfPackageNotInstalled(findPreference(KEY_CHRONUS));
  90.  
  91.         mPowerButtonTorch = (CheckBoxPreference) findPreference(KEY_POWER_BUTTON_TORCH);
  92.         if (torchSupported()) {
  93.             mPowerButtonTorch.setChecked((Settings.System.getInt(getActivity().
  94.                     getApplicationContext().getContentResolver(),
  95.                     Settings.System.POWER_BUTTON_TORCH, 0) == 1));
  96.         } else {
  97.             getPreferenceScreen().removePreference(mPowerButtonTorch);
  98.         }
  99.  
  100.         mDualpane = (CheckBoxPreference) findPreference(PREF_FORCE_DUAL_PANEL);
  101.             mDualpane.setOnPreferenceChangeListener(this);
  102.  
  103.     }
  104.  
  105.     @Override
  106.     public void onResume() {
  107.         super.onResume();
  108.     }
  109.  
  110.     @Override
  111.     public void onPause() {
  112.         super.onPause();
  113.     }
  114.  
  115.     @Override
  116.     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
  117.         if (preference == mPowerButtonTorch) {
  118.             boolean enabled = mPowerButtonTorch.isChecked();
  119.             Settings.System.putInt(getContentResolver(), Settings.System.POWER_BUTTON_TORCH,
  120.                     enabled ? 1 : 0);
  121.             return true;
  122.         } else {
  123.             return super.onPreferenceTreeClick(preferenceScreen, preference);
  124.         }
  125.     }
  126.  
  127.     public boolean onPreferenceChange(Preference preference, Object objValue) {
  128.         ContentResolver cr = getActivity().getContentResolver();
  129.  
  130.         if (preference == mDualpane) {
  131.             Settings.System.putInt(getActivity().getContentResolver(),
  132.                     Settings.System.FORCE_DUAL_PANEL,
  133.                     ((CheckBoxPreference)preference).isChecked() ? 0 : 1);
  134.             return true;
  135.         } else if (preference == mCustomLabel) {
  136.             AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
  137.  
  138.             alert.setTitle(R.string.custom_carrier_label_title);
  139.             alert.setMessage(R.string.custom_carrier_label_explain);
  140.  
  141.             // Set an EditText view to get user input
  142.             final EditText input = new EditText(getActivity());
  143.             input.setText(mCustomLabelText != null ? mCustomLabelText : "");
  144.             alert.setView(input);
  145.             alert.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
  146.  
  147.                 public void onClick(DialogInterface dialog, int whichButton) {
  148.                     String value = ((Spannable) input.getText()).toString();
  149.                     Settings.System.putString(getActivity().getContentResolver(),
  150.                             Settings.System.CUSTOM_CARRIER_LABEL, value);
  151.                     updateCustomLabelTextSummary();
  152.                     Intent i = new Intent();
  153.                     i.setAction("com.android.settings.LABEL_CHANGED");
  154.                     mContext.sendBroadcast(i);
  155.                 }
  156.             });
  157.             alert.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
  158.                 public void onClick(DialogInterface dialog, int whichButton) {
  159.                     // Canceled.
  160.                 }
  161.             });
  162.  
  163.             alert.show();  
  164.         }
  165.         return false;
  166.     }
  167.  
  168.     private boolean removePreferenceIfPackageNotInstalled(Preference preference) {
  169.         String intentUri=((PreferenceScreen) preference).getIntent().toUri(1);
  170.         Pattern pattern = Pattern.compile("component=([^/]+)/");
  171.         Matcher matcher = pattern.matcher(intentUri);
  172.  
  173.         String packageName=matcher.find()?matcher.group(1):null;
  174.         if(packageName != null) {
  175.             try {
  176.                 getPackageManager().getPackageInfo(packageName, 0);
  177.             } catch (NameNotFoundException e) {
  178.                 Log.e(TAG,"package "+packageName+" not installed, hiding preference.");
  179.                 getPreferenceScreen().removePreference(preference);
  180.                 return true;
  181.             }
  182.         }
  183.         return false;
  184.     }
  185.  
  186.     private void updateCustomLabelTextSummary() {
  187.         mCustomLabelText = Settings.System.getString(getActivity().getContentResolver(),
  188.                 Settings.System.CUSTOM_CARRIER_LABEL);
  189.         if (mCustomLabelText == null || mCustomLabelText.length() == 0) {
  190.             mCustomLabel.setSummary(R.string.custom_carrier_label_notset);
  191.         } else {
  192.             mCustomLabel.setSummary(mCustomLabelText);
  193.         }
  194.     }  
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement