diff options
Diffstat (limited to 'AriesParts/src')
5 files changed, 300 insertions, 0 deletions
diff --git a/AriesParts/src/com/cyanogenmod/AriesParts/AriesParts.java b/AriesParts/src/com/cyanogenmod/AriesParts/AriesParts.java new file mode 100644 index 0000000..9aa3916 --- /dev/null +++ b/AriesParts/src/com/cyanogenmod/AriesParts/AriesParts.java @@ -0,0 +1,28 @@ +package com.cyanogenmod.AriesParts; + +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.PreferenceActivity; + +public class AriesParts extends PreferenceActivity { + + public static final String KEY_COLOR_TUNING = "color_tuning"; + public static final String KEY_BACKLIGHT_TIMEOUT = "backlight_timeout"; + + private ColorTuningPreference mColorTuning; + private ListPreference mBacklightTimeout; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.main); + + mColorTuning = (ColorTuningPreference) findPreference(KEY_COLOR_TUNING); + mColorTuning.setEnabled(ColorTuningPreference.isSupported()); + + mBacklightTimeout = (ListPreference) findPreference(KEY_BACKLIGHT_TIMEOUT); + mBacklightTimeout.setEnabled(TouchKeyBacklightTimeout.isSupported()); + mBacklightTimeout.setOnPreferenceChangeListener(new TouchKeyBacklightTimeout()); + } + +} diff --git a/AriesParts/src/com/cyanogenmod/AriesParts/ColorTuningPreference.java b/AriesParts/src/com/cyanogenmod/AriesParts/ColorTuningPreference.java new file mode 100644 index 0000000..edffa72 --- /dev/null +++ b/AriesParts/src/com/cyanogenmod/AriesParts/ColorTuningPreference.java @@ -0,0 +1,173 @@ +package com.cyanogenmod.AriesParts; + +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.widget.SeekBar; +import android.widget.TextView; + +/** + * Special preference type that allows configuration of both the ring volume and + * notification volume. + */ +public class ColorTuningPreference extends DialogPreference { + + enum Colors { + RED, + GREEN, + BLUE + }; + + private static final int[] SEEKBAR_ID = new int[] { + R.id.color_red_seekbar, + R.id.color_green_seekbar, + R.id.color_blue_seekbar + }; + + private static final int[] VALUE_DISPLAY_ID = new int[] { + R.id.color_red_value, + R.id.color_green_value, + R.id.color_blue_value + }; + + private static final String[] FILE_PATH = new String[] { + "/sys/devices/virtual/misc/color_tuning/red_multiplier", + "/sys/devices/virtual/misc/color_tuning/green_multiplier", + "/sys/devices/virtual/misc/color_tuning/blue_multiplier" + }; + + private ColorSeekBar mSeekBars[] = new ColorSeekBar[3]; + + private static final int MAX_VALUE = Integer.MAX_VALUE; + + // Track instances to know when to restore original color + // (when the orientation changes, a new dialog is created before the old one is destroyed) + private static int sInstances = 0; + + public ColorTuningPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + setDialogLayoutResource(R.layout.preference_dialog_color_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 ColorSeekBar(seekBar, valueDisplay, FILE_PATH[i]); + } + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + super.onDialogClosed(positiveResult); + + sInstances--; + + if (positiveResult) { + for (ColorSeekBar csb : mSeekBars) { + csb.save(); + } + } else if (sInstances == 0) { + for (ColorSeekBar csb : mSeekBars) { + csb.reset(); + } + } + } + + /** + * Restore screen color tuning from SharedPreferences. (Write to kernel.) + * @param context The context to read the SharedPreferences from + */ + public static void restore(Context context) { + if (!isSupported()) { + return; + } + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + for (String filePath : FILE_PATH) { + int value = sharedPrefs.getInt(filePath, MAX_VALUE); + Utils.writeColor(filePath, value); + } + } + + /** + * Check whether the running kernel supports color tuning or not. + * @return Whether color 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 ColorSeekBar implements SeekBar.OnSeekBarChangeListener { + + private String mFilePath; + private int mOriginal; + private SeekBar mSeekBar; + private TextView mValueDisplay; + + public ColorSeekBar(SeekBar seekBar, TextView valueDisplay, String filePath) { + mSeekBar = seekBar; + mValueDisplay = valueDisplay; + mFilePath = filePath; + + // Read original value + SharedPreferences sharedPreferences = getSharedPreferences(); + mOriginal = sharedPreferences.getInt(mFilePath, MAX_VALUE); + + seekBar.setMax(MAX_VALUE); + reset(); + seekBar.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.writeColor(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("%.3f", (double) progress / MAX_VALUE)); + } + + } + +} diff --git a/AriesParts/src/com/cyanogenmod/AriesParts/Startup.java b/AriesParts/src/com/cyanogenmod/AriesParts/Startup.java new file mode 100644 index 0000000..540ed9b --- /dev/null +++ b/AriesParts/src/com/cyanogenmod/AriesParts/Startup.java @@ -0,0 +1,15 @@ +package com.cyanogenmod.AriesParts; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class Startup extends BroadcastReceiver { + + @Override + public void onReceive(final Context context, final Intent bootintent) { + ColorTuningPreference.restore(context); + TouchKeyBacklightTimeout.restore(context); + } + +} diff --git a/AriesParts/src/com/cyanogenmod/AriesParts/TouchKeyBacklightTimeout.java b/AriesParts/src/com/cyanogenmod/AriesParts/TouchKeyBacklightTimeout.java new file mode 100644 index 0000000..554098a --- /dev/null +++ b/AriesParts/src/com/cyanogenmod/AriesParts/TouchKeyBacklightTimeout.java @@ -0,0 +1,36 @@ +package com.cyanogenmod.AriesParts; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceChangeListener; +import android.preference.PreferenceManager; + +public class TouchKeyBacklightTimeout implements OnPreferenceChangeListener { + + private static final String FILE = "/sys/class/misc/notification/bl_timeout"; + + public static boolean isSupported() { + return Utils.fileExists(FILE); + } + + /** + * Restore backlight timeout setting from SharedPreferences. (Write to kernel.) + * @param context The context to read the SharedPreferences from + */ + public static void restore(Context context) { + if (!isSupported()) { + return; + } + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + Utils.writeValue(FILE, sharedPrefs.getString(AriesParts.KEY_BACKLIGHT_TIMEOUT, "1600")); + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Utils.writeValue(FILE, (String) newValue); + return true; + } + +} diff --git a/AriesParts/src/com/cyanogenmod/AriesParts/Utils.java b/AriesParts/src/com/cyanogenmod/AriesParts/Utils.java new file mode 100644 index 0000000..9c07261 --- /dev/null +++ b/AriesParts/src/com/cyanogenmod/AriesParts/Utils.java @@ -0,0 +1,48 @@ +package com.cyanogenmod.AriesParts; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +public class Utils { + + /** + * Write a string value to the specified file. + * @param filename The filename + * @param value The value + */ + public static void writeValue(String filename, String value) { + try { + FileOutputStream fos = new FileOutputStream(new File(filename)); + fos.write(value.getBytes()); + fos.flush(); + fos.getFD().sync(); + fos.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Write the "color value" to the specified file. The value is scaled from + * an integer to an unsigned integer by multiplying by 2. + * @param filename The filename + * @param value The value of max value Integer.MAX + */ + public static void writeColor(String filename, int value) { + writeValue(filename, String.valueOf((long) value * 2)); + } + + /** + * Check if the specified file exists. + * @param filename The filename + * @return Whether the file exists or not + */ + public static boolean fileExists(String filename) { + return new File(filename).exists(); + } + +} |