Advertisement
csphelps76

Untitled

Feb 8th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.45 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 if (preference == mCustomLabel) {
  123.             AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
  124.  
  125.             alert.setTitle(R.string.custom_carrier_label_title);
  126.             alert.setMessage(R.string.custom_carrier_label_explain);
  127.  
  128.             // Set an EditText view to get user input
  129.             final EditText input = new EditText(getActivity());
  130.             input.setText(mCustomLabelText != null ? mCustomLabelText : "");
  131.             alert.setView(input);
  132.             alert.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
  133.  
  134.                 public void onClick(DialogInterface dialog, int whichButton) {
  135.                     String value = ((Spannable) input.getText()).toString();
  136.                     Settings.System.putString(getActivity().getContentResolver(),
  137.                             Settings.System.CUSTOM_CARRIER_LABEL, value);
  138.                     updateCustomLabelTextSummary();
  139.                     Intent i = new Intent();
  140.                     i.setAction("com.android.settings.LABEL_CHANGED");
  141.                     mContext.sendBroadcast(i);
  142.                 }
  143.             });
  144.             alert.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
  145.                 public void onClick(DialogInterface dialog, int whichButton) {
  146.                     // Canceled.
  147.                 }
  148.             });
  149.  
  150.             alert.show();  
  151.         }
  152.         return super.onPreferenceTreeClick(preferenceScreen, preference);
  153.     }
  154.  
  155.     public boolean onPreferenceChange(Preference preference, Object objValue) {
  156.         ContentResolver cr = getActivity().getContentResolver();
  157.  
  158.         if (preference == mDualpane) {
  159.             Settings.System.putInt(getActivity().getContentResolver(),
  160.                     Settings.System.FORCE_DUAL_PANEL,
  161.                     ((CheckBoxPreference)preference).isChecked() ? 0 : 1);
  162.             return true;
  163.         }
  164.         return false;
  165.     }
  166.  
  167.     private boolean removePreferenceIfPackageNotInstalled(Preference preference) {
  168.         String intentUri=((PreferenceScreen) preference).getIntent().toUri(1);
  169.         Pattern pattern = Pattern.compile("component=([^/]+)/");
  170.         Matcher matcher = pattern.matcher(intentUri);
  171.  
  172.         String packageName=matcher.find()?matcher.group(1):null;
  173.         if(packageName != null) {
  174.             try {
  175.                 getPackageManager().getPackageInfo(packageName, 0);
  176.             } catch (NameNotFoundException e) {
  177.                 Log.e(TAG,"package "+packageName+" not installed, hiding preference.");
  178.                 getPreferenceScreen().removePreference(preference);
  179.                 return true;
  180.             }
  181.         }
  182.         return false;
  183.     }
  184.  
  185.     private void updateCustomLabelTextSummary() {
  186.         mCustomLabelText = Settings.System.getString(getActivity().getContentResolver(),
  187.                 Settings.System.CUSTOM_CARRIER_LABEL);
  188.         if (mCustomLabelText == null || mCustomLabelText.length() == 0) {
  189.             mCustomLabel.setSummary(R.string.custom_carrier_label_notset);
  190.         } else {
  191.             mCustomLabel.setSummary(mCustomLabelText);
  192.         }
  193.     }  
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement