From b573633abeb138af9dfea62266f19dd9250e59da Mon Sep 17 00:00:00 2001 From: KalimochoAz Date: Sat, 1 Sep 2012 14:51:29 +0200 Subject: Add button led timeout option into settings Change-Id: If9ca126b52fd06da7f6e55fc59b305189acac68a --- .../res/layout/preference_dialog_bld_tuning.xml | 35 ++++ CrespoParts/res/values/strings.xml | 3 + CrespoParts/res/xml/general_preferences.xml | 4 + .../settings/device/DeviceSettings.java | 1 + .../settings/device/GeneralFragmentActivity.java | 9 +- .../com/cyanogenmod/settings/device/Startup.java | 1 + .../src/com/cyanogenmod/settings/device/Utils.java | 15 ++ .../settings/device/bldTuningPreference.java | 218 +++++++++++++++++++++ 8 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 CrespoParts/res/layout/preference_dialog_bld_tuning.xml create mode 100644 CrespoParts/src/com/cyanogenmod/settings/device/bldTuningPreference.java (limited to 'CrespoParts') diff --git a/CrespoParts/res/layout/preference_dialog_bld_tuning.xml b/CrespoParts/res/layout/preference_dialog_bld_tuning.xml new file mode 100644 index 0000000..5c83b6e --- /dev/null +++ b/CrespoParts/res/layout/preference_dialog_bld_tuning.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + diff --git a/CrespoParts/res/values/strings.xml b/CrespoParts/res/values/strings.xml index 661fb8d..d3531df 100644 --- a/CrespoParts/res/values/strings.xml +++ b/CrespoParts/res/values/strings.xml @@ -26,6 +26,9 @@ Default CM Sett. Dark Sett. + BLD + BLD Tuning + Set the time to turn off keys light ( 0 means no BLD ) Default. Natural diff --git a/CrespoParts/res/xml/general_preferences.xml b/CrespoParts/res/xml/general_preferences.xml index 423f97b..7788a88 100644 --- a/CrespoParts/res/xml/general_preferences.xml +++ b/CrespoParts/res/xml/general_preferences.xml @@ -20,6 +20,10 @@ + diff --git a/CrespoParts/src/com/cyanogenmod/settings/device/DeviceSettings.java b/CrespoParts/src/com/cyanogenmod/settings/device/DeviceSettings.java index d84a78b..e868086 100644 --- a/CrespoParts/src/com/cyanogenmod/settings/device/DeviceSettings.java +++ b/CrespoParts/src/com/cyanogenmod/settings/device/DeviceSettings.java @@ -49,6 +49,7 @@ public class DeviceSettings extends Activity { public static final String KEY_NOTIFICATION = "touchkey_notification"; public static final String KEY_CATEGORY_RADIO = "category_radio"; public static final String KEY_HSPA = "hspa"; + public static final String KEY_BLD_TUNING = "bld_tuning"; ViewPager mViewPager; TabsAdapter mTabsAdapter; diff --git a/CrespoParts/src/com/cyanogenmod/settings/device/GeneralFragmentActivity.java b/CrespoParts/src/com/cyanogenmod/settings/device/GeneralFragmentActivity.java index eb183a2..d437997 100644 --- a/CrespoParts/src/com/cyanogenmod/settings/device/GeneralFragmentActivity.java +++ b/CrespoParts/src/com/cyanogenmod/settings/device/GeneralFragmentActivity.java @@ -44,9 +44,11 @@ public class GeneralFragmentActivity extends PreferenceFragment implements OnPre private static final String TOUCHKEY_NOTIFICATION_FILE = "/sys/class/misc/notification/enabled"; private static final String PREF_ENABLED = "1"; private static final String TAG = "CrespoParts_General"; + private static final String BLD_FILE = "/sys/class/misc/backlightdimmer/enabled"; private CheckBoxPreference mDeepIdle; private CheckBoxPreference mNotification; + private bldTuningPreference mbldTuning; @Override public void onCreate(Bundle savedInstanceState) { @@ -71,9 +73,12 @@ public class GeneralFragmentActivity extends PreferenceFragment implements OnPre } Preference p = findPreference(DeviceSettings.KEY_DEEPIDLE_STATS); - if(p != null) { + if(p != null) p.setOnPreferenceClickListener(this); - } + + mbldTuning = (bldTuningPreference) findPreference(DeviceSettings.KEY_BLD_TUNING); + if(mbldTuning != null) + mbldTuning.setEnabled(bldTuningPreference.isSupported()); } diff --git a/CrespoParts/src/com/cyanogenmod/settings/device/Startup.java b/CrespoParts/src/com/cyanogenmod/settings/device/Startup.java index 0728953..2799424 100644 --- a/CrespoParts/src/com/cyanogenmod/settings/device/Startup.java +++ b/CrespoParts/src/com/cyanogenmod/settings/device/Startup.java @@ -10,6 +10,7 @@ public class Startup extends BroadcastReceiver { public void onReceive(final Context context, final Intent bootintent) { GeneralFragmentActivity.restore(context); ColorTuningPreference.restore(context); + bldTuningPreference.restore(context); GammaTuningPreference.restore(context); if (Hspa.isSupported()) { Hspa.restore(context); diff --git a/CrespoParts/src/com/cyanogenmod/settings/device/Utils.java b/CrespoParts/src/com/cyanogenmod/settings/device/Utils.java index 6b9c0d3..a5b9ea7 100644 --- a/CrespoParts/src/com/cyanogenmod/settings/device/Utils.java +++ b/CrespoParts/src/com/cyanogenmod/settings/device/Utils.java @@ -119,6 +119,21 @@ public class Utils { } /** + * Write the "color value" to the specified file. The value is scaled from + * seconds to miliseconds multiplying by 1000. + * + * @param filename The filename + * @param value The value of time + */ + public static void writebld(String filename, int value) { + writeValue(filename, String.valueOf(value * 1000)); + if(value > 0) + writeValue("/sys/class/misc/backlightdimmer/enabled", "1"); + else + writeValue("/sys/class/misc/backlightdimmer/enabled", "0"); + } + + /** * Check if the specified file exists. * * @param filename The filename diff --git a/CrespoParts/src/com/cyanogenmod/settings/device/bldTuningPreference.java b/CrespoParts/src/com/cyanogenmod/settings/device/bldTuningPreference.java new file mode 100644 index 0000000..edad9ba --- /dev/null +++ b/CrespoParts/src/com/cyanogenmod/settings/device/bldTuningPreference.java @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2011 The CyanogenMod 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.cyanogenmod.settings.device; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.preference.DialogPreference; +import android.preference.PreferenceManager; +import android.util.AttributeSet; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.SeekBar; +import android.widget.TextView; +import android.util.Log; +import android.widget.Button; + +/** + * Special preference type that allows configuration of both the ring volume and + * notification volume. + */ +public class bldTuningPreference extends DialogPreference { + + private static final String TAG = "BLD..."; + + private static final int[] SEEKBAR_ID = new int[] { + R.id.bld_seekbar + }; + + private static final int[] VALUE_DISPLAY_ID = new int[] { + R.id.bld_value + }; + + private static final String[] FILE_PATH = new String[] { + "/sys/class/misc/backlightdimmer/delay", + }; + + + private bldSeekBar mSeekBars[] = new bldSeekBar[1]; + + // Align MAX_VALUE with Voodoo Control settings + private static final int MAX_VALUE = 30; + + // Track instances to know when to restore original bld + // (when the orientation changes, a new dialog is created before the old one + // is destroyed) + private static int sInstances = 0; + + public bldTuningPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + setDialogLayoutResource(R.layout.preference_dialog_bld_tuning); + } + + @Override + protected void onBindDialogView(View view) { + super.onBindDialogView(view); + + sInstances++; + + for (int i = 0; i < SEEKBAR_ID.length; i++) { + SeekBar seekBar = (SeekBar) view.findViewById(SEEKBAR_ID[i]); + TextView valueDisplay = (TextView) view.findViewById(VALUE_DISPLAY_ID[i]); + mSeekBars[i] = new bldSeekBar(seekBar, valueDisplay, FILE_PATH[i]); + } + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + super.onDialogClosed(positiveResult); + + sInstances--; + + if (positiveResult) { + for (bldSeekBar csb : mSeekBars) { + csb.save(); + } + } else if (sInstances == 0) { + for (bldSeekBar csb : mSeekBars) { + csb.reset(); + } + } + } + + /** + * Restore screen bld tuning from SharedPreferences. (Write to kernel.) + * + * @param context The context to read the SharedPreferences from + */ + public static void restore(Context context) { + int iValue, iValue2; + if (!isSupported()) { + return; + } + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + + for (String filePath : FILE_PATH) { + String sDefaultValue = Utils.readOneLine(filePath); + Log.d(TAG,"INIT: " + sDefaultValue); + try { + iValue2 = Integer.parseInt(sDefaultValue) - 1000; + } catch (NumberFormatException e) { + iValue2 = 0; + } + try { + iValue = sharedPrefs.getInt(filePath, iValue2); + Log.d(TAG, "restore: iValue: " + iValue + " File: " + filePath); + } catch (NumberFormatException e) { + iValue = iValue2; + Log.e(TAG, "restore ERROR: iValue: " + iValue + " File: " + filePath); + } + Utils.writebld(filePath, (int) iValue); + } + } + + /** + * Check whether the running kernel supports bld tuning or not. + * + * @return Whether bld tuning is supported or not + */ + public static boolean isSupported() { + boolean supported = true; + for (String filePath : FILE_PATH) { + if (!Utils.fileExists(filePath)) { + supported = false; + } + } + + return supported; + } + + class bldSeekBar implements SeekBar.OnSeekBarChangeListener { + + private String mFilePath; + + private int mOriginal; + + private SeekBar mSeekBar; + + private TextView mValueDisplay; + + public bldSeekBar(SeekBar seekBar, TextView valueDisplay, String filePath) { + int iValue; + + mSeekBar = seekBar; + mValueDisplay = valueDisplay; + mFilePath = filePath; + + SharedPreferences sharedPreferences = getSharedPreferences(); + + // Read original value + if (Utils.fileExists(mFilePath)) { + String sDefaultValue = Utils.readOneLine(mFilePath); + iValue = (int) (Long.valueOf(sDefaultValue) / 1000); + } else { + iValue = sharedPreferences.getInt(mFilePath, MAX_VALUE); + } + mOriginal = iValue; + + mSeekBar.setMax(MAX_VALUE); + reset(); + mSeekBar.setOnSeekBarChangeListener(this); + } + + public void reset() { + mSeekBar.setProgress(mOriginal); + updateValue(mOriginal); + } + + public void save() { + Editor editor = getEditor(); + editor.putInt(mFilePath, mSeekBar.getProgress()); + editor.commit(); + } + + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + Utils.writebld(mFilePath, progress); + updateValue(progress); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + // Do nothing + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + // Do nothing + } + + private void updateValue(int progress) { + mValueDisplay.setText(String.format("%d", progress)); + } + + public void SetNewValue(int iValue) { + mOriginal = iValue; + reset(); + } + + } + +} -- cgit v1.1