Skip to content

Commit

Permalink
Merge "Re-add auto brightness configuration UI (2/2)." into cm-10.2
Browse files Browse the repository at this point in the history
  • Loading branch information
DvTonder authored and Gerrit Code Review committed Aug 5, 2013
2 parents 9707273 + 9e84e98 commit c134412
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 0 deletions.
4 changes: 4 additions & 0 deletions AndroidManifest.xml
Expand Up @@ -1620,6 +1620,10 @@
android:resource="@id/system_settings" />
</activity>

<activity android:name=".cyanogenmod.AutoBrightnessSetup"
android:excludeFromRecents="true"
android:theme="@*android:style/Theme.Holo.Dialog.Alert" />

<activity android:name=".cyanogenmod.PerformanceSettings" />

<activity android:name="Settings$QuietHoursSettingsActivity"
Expand Down
72 changes: 72 additions & 0 deletions res/layout/dialog_auto_brightness.xml
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source 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.
Copyright (C) 2009 Motorola, Inc.
March 23, 2009 - Motorola - Allow automatic brightness changes.
-->

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:paddingBottom="20dip">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="20dip"
android:text="@string/auto_brightness_adjustment_title" />

<com.android.settings.cyanogenmod.MagneticCenterSeekBar android:id="@+id/adjustment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:max="10000" />

<CheckBox android:id="@+id/twilight_adjustment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="20dip"
android:text="@string/auto_brightness_twilight_adjustment" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="20dip"
android:text="@string/auto_brightness_sensitivity_title" />

<Spinner android:id="@+id/sensitivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip" />

</LinearLayout>

</ScrollView>
3 changes: 3 additions & 0 deletions res/values/cm_strings.xml
Expand Up @@ -833,6 +833,9 @@
<string name="cyanogenmod_waiver_body">Changing this setting may be against your state or government laws.\n\nIn selecting \'OK\' below, you acknowledge this warning and waive, indemnify and hold harmless CyanogenMod and its contributors from all legal liabilities.</string>

<!-- Automatic brightness customization -->
<string name="auto_brightness_setup_title">Automatic brightness</string>
<string name="auto_brightness_adjustment_title">Brightness adjustment</string>
<string name="auto_brightness_twilight_adjustment">Adjust to sunrise and sunset</string>
<string name="light_sensor_current_value">Current light sensor value: <xliff:g id="light_level">%d</xliff:g> lux</string>
<string name="auto_brightness_dialog_title">Automatic brightness levels</string>
<string name="auto_brightness_level_format"><xliff:g id="level">%1$d</xliff:g> lux</string>
Expand Down
185 changes: 185 additions & 0 deletions src/com/android/settings/cyanogenmod/AutoBrightnessSetup.java
@@ -0,0 +1,185 @@
/*
* Copyright (C) 2013 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.android.settings.cyanogenmod;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.Spinner;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.settings.R;

public class AutoBrightnessSetup extends AlertActivity implements
DialogInterface.OnClickListener {
private static final String STATE_TWILIGHT = "AutoBrightnessSetup:TwilightAdjustment";
private static final String STATE_SENSITIVITY = "AutoBrightnessSetup:SensitivitySelection";
private static final String STATE_ADJUSTMENT = "AutoBrightnessSetup:AdjustmentValue";
private static final String STATE_CUSTOMIZE_SHOWN = "AutoBrightnessSetup:CustomizeDialogShown";
private static final String STATE_CUSTOMIZE_STATE = "AutoBrightnessSetup:CustomizeDialogState";

private AutoBrightnessCustomizeDialog mCustomizeDialog;
private CheckBox mTwilightAdjustment;
private Spinner mSensitivity;
private SeekBar mAdjustment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final View view = getLayoutInflater().inflate(R.layout.dialog_auto_brightness, null);
final AlertController.AlertParams p = mAlertParams;
p.mIconId = R.drawable.ic_appwidget_settings_brightness_auto_holo;
p.mTitle = getString(R.string.auto_brightness_setup_title);
p.mView = view;
p.mPositiveButtonText = getString(android.R.string.ok);
p.mPositiveButtonListener = this;
p.mNeutralButtonText = getString(R.string.auto_brightness_adjust_button);
p.mNeutralButtonListener = this;
p.mNegativeButtonText = getString(android.R.string.cancel);
p.mNegativeButtonListener = this;

setupAlert();

mTwilightAdjustment = (CheckBox) view.findViewById(R.id.twilight_adjustment);
mSensitivity = (Spinner) view.findViewById(R.id.sensitivity);
mAdjustment = (SeekBar) view.findViewById(R.id.adjustment);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.auto_brightness_sensitivity_entries,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSensitivity.setAdapter(adapter);

Button adjustButton = mAlert.getButton(BUTTON_NEUTRAL);
adjustButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomizeDialog(null);
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mCustomizeDialog != null) {
mCustomizeDialog.dismiss();
}
}

@Override
protected void onStart() {
super.onStart();

final ContentResolver resolver = getContentResolver();
float currentSensitivity = Settings.System.getFloat(resolver,
Settings.System.AUTO_BRIGHTNESS_RESPONSIVENESS, 1.0f);
int currentSensitivityInt = (int) (currentSensitivity * 100);
int[] sensitivityValues = getResources().getIntArray(
R.array.auto_brightness_sensitivity_values);

for (int i = 0; i < sensitivityValues.length; i++) {
if (sensitivityValues[i] == currentSensitivityInt) {
mSensitivity.setSelection(i);
break;
}
}

float adjustmentValue = Settings.System.getFloat(resolver,
Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f);
// valid range is -1.0..1.0, but we clamp the extreme values
adjustmentValue = Math.min(Math.max(adjustmentValue, -0.5f), 0.5f);
mAdjustment.setProgress((int) ((adjustmentValue + 0.5f) * mAdjustment.getMax()));

mTwilightAdjustment.setChecked(Settings.System.getInt(resolver,
Settings.System.AUTO_BRIGHTNESS_TWILIGHT_ADJUSTMENT, 0) != 0);
}

@Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);

boolean customizeDialogShown = mCustomizeDialog != null && mCustomizeDialog.isShowing();
state.putBoolean(STATE_CUSTOMIZE_SHOWN, customizeDialogShown);
if (customizeDialogShown) {
state.putBundle(STATE_CUSTOMIZE_STATE, mCustomizeDialog.onSaveInstanceState());
}
state.putInt(STATE_SENSITIVITY, mSensitivity.getSelectedItemPosition());
state.putInt(STATE_ADJUSTMENT, mAdjustment.getProgress());
state.putBoolean(STATE_TWILIGHT, mTwilightAdjustment.isChecked());
}

@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);

if (state.getBoolean(STATE_CUSTOMIZE_SHOWN)) {
Bundle dialogState = state.getBundle(STATE_CUSTOMIZE_STATE);
showCustomizeDialog(dialogState);
}
mSensitivity.setSelection(state.getInt(STATE_SENSITIVITY));
mAdjustment.setProgress(state.getInt(STATE_ADJUSTMENT));
mTwilightAdjustment.setChecked(state.getBoolean(STATE_TWILIGHT));
}

@Override
public void onClick(DialogInterface dialog, int which) {
if (which != DialogInterface.BUTTON_POSITIVE) {
return;
}

final ContentResolver resolver = getContentResolver();
int selection = mSensitivity.getSelectedItemPosition();
if (selection >= 0) {
int[] sensitivityValues = getResources().getIntArray(
R.array.auto_brightness_sensitivity_values);
float sensitivity = 0.01f * sensitivityValues[selection];

Settings.System.putFloat(resolver,
Settings.System.AUTO_BRIGHTNESS_RESPONSIVENESS, sensitivity);
}

float adjustmentValue =
((float) mAdjustment.getProgress() / (float) mAdjustment.getMax()) - 0.5f;
Settings.System.putFloat(resolver,
Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, adjustmentValue);

Settings.System.putInt(resolver,
Settings.System.AUTO_BRIGHTNESS_TWILIGHT_ADJUSTMENT,
mTwilightAdjustment.isChecked() ? 1 : 0);
}

private void showCustomizeDialog(Bundle state) {
if (mCustomizeDialog != null && mCustomizeDialog.isShowing()) {
return;
}

mCustomizeDialog = new AutoBrightnessCustomizeDialog(this);
if (state != null) {
mCustomizeDialog.onRestoreInstanceState(state);
}
mCustomizeDialog.show();
}
}
62 changes: 62 additions & 0 deletions src/com/android/settings/cyanogenmod/MagneticCenterSeekBar.java
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2013 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.android.settings.cyanogenmod;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.SeekBar;

public class MagneticCenterSeekBar extends SeekBar {
private static final float CENTER_SNAP_IN_THRESHOLD = 0.03f;
private static final float CENTER_SNAP_OUT_THRESHOLD = 0.03f;

public MagneticCenterSeekBar(Context context) {
super(context);
}

public MagneticCenterSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MagneticCenterSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected int updateTouchProgress(int lastProgress, int newProgress) {
int centerProgress = getMax() / 2;
int inThreshold = (int) (CENTER_SNAP_IN_THRESHOLD * getMax());
int outThreshold = (int) (CENTER_SNAP_OUT_THRESHOLD * getMax());
boolean resetToCenter = false;

if (newProgress > lastProgress) {
resetToCenter =
newProgress < centerProgress && newProgress > centerProgress - inThreshold
|| newProgress > centerProgress && newProgress < centerProgress + outThreshold;
} else if (newProgress < lastProgress) {
resetToCenter =
newProgress > centerProgress && newProgress < centerProgress + inThreshold
|| newProgress < centerProgress && newProgress > centerProgress + outThreshold;
}

if (resetToCenter) {
return centerProgress;
}

return newProgress;
}
}

0 comments on commit c134412

Please sign in to comment.