|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
/*
* Copyright (C) 2010 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.
*/
package com.android.settings.dualscreen;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.content.res.Configuration;
import android.os.RemoteException;
import android.util.Slog;
import com.android.settings.R;
import com.android.settings.WirelessSettings;
/**
* BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
* preference. It turns on/off Bluetooth and ensures the summary of the
* preference reflects the current state.
*/
public final class DualScreenEnabler implements CompoundButton.OnCheckedChangeListener {
private static final String LOG = "DualScreenEnabler";
private static final boolean DEBUG = true;
private void LOGD(String msg){
if(DEBUG){
Slog.d(LOG, msg);
}
}
private final Context mContext;
private Switch mSwitch;
private boolean mValidListener;
private final String store_key = Settings.System.DUAL_SCREEN_MODE;
private final String icon_key = Settings.System.DUAL_SCREEN_ICON_USED;
private final int DUAL_SCREEN_CLOSE = 0;
private final int DUAL_SCREEN_OPEN = 1;
public DualScreenEnabler(Context context, Switch switch_) {
mContext = context;
mSwitch = switch_;
mValidListener = false;
}
private int getDualScreenValue(){
return Settings.System.getInt(
mContext.getContentResolver(), store_key, DUAL_SCREEN_CLOSE);
}
private final ContentObserver mValueObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
final boolean enable = (DUAL_SCREEN_CLOSE != getDualScreenValue());
LOGD("onchagne enable="+enable + "selfChange="+selfChange);
try {
IActivityManager am = ActivityManagerNative.getDefault();
Configuration config = am.getConfiguration();
// Will set userSetLocale to indicate this isn't some passing default - the user
// wants this remembered
config.setDualScreenFlag(enable);
am.updateConfiguration(config);
// Trigger the dirty bit for the Settings Provider.
//BackupManager.dataChanged("com.android.providers.settings");
} catch (RemoteException e) {
// Intentionally left blank
}
}
};
public void resume() {
LOGD("--------resume--------------"+getDualScreenValue());
handleStateChanged(getDualScreenValue());
mSwitch.setOnCheckedChangeListener(this);
mContext.getContentResolver().registerContentObserver(
Settings.System.getUriFor(store_key), false,
mValueObserver);
mValidListener = true;
}
public void pause() {
LOGD("--------pause--------------"+getDualScreenValue());
mSwitch.setOnCheckedChangeListener(null);
mContext.getContentResolver().unregisterContentObserver(mValueObserver);
mValidListener = false;
}
void handleStateChanged(int state) {
switch (state) {
case DUAL_SCREEN_CLOSE:
setChecked(false);
mSwitch.setEnabled(true);
break;
case DUAL_SCREEN_OPEN:
setChecked(true);
mSwitch.setEnabled(true);
break;
}
}
public void setSwitch(Switch switch_) {
LOGD("setSwitch switch=");
if (mSwitch == switch_) return;
mSwitch.setOnCheckedChangeListener(null);
mContext.getContentResolver().unregisterContentObserver(mValueObserver);
mSwitch = switch_;
mSwitch.setOnCheckedChangeListener(this);
mContext.getContentResolver().registerContentObserver(
Settings.System.getUriFor(store_key), false,
mValueObserver);
boolean isOn = getDualScreenValue() != DUAL_SCREEN_CLOSE;
setChecked(isOn);
mSwitch.setEnabled(true);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// mSwitch.setEnabled(true);
LOGD("-----------------------------isChecked:"+isChecked);
int value = DUAL_SCREEN_CLOSE;
if(isChecked){
value = DUAL_SCREEN_OPEN;
} else {
Settings.System.putInt(mContext.getContentResolver(), icon_key, 0);
}
Settings.System.putInt(
mContext.getContentResolver(), store_key, value);
}
private void setChecked(boolean isChecked) {
if (isChecked != mSwitch.isChecked()) {
// set listener to null, so onCheckedChanged won't be called
// if the checked status on Switch isn't changed by user click
if (mValidListener) {
mSwitch.setOnCheckedChangeListener(null);
}
mSwitch.setChecked(isChecked);
if (mValidListener) {
mSwitch.setOnCheckedChangeListener(this);
}
}
}
} |
|