1/* 2 * Copyright (C) 2010 The Android Open Source Project 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 17package com.android.systemui.statusbar.policy; 18 19import android.content.ContentResolver; 20import android.content.Context; 21import android.os.AsyncTask; 22import android.os.RemoteException; 23import android.os.ServiceManager; 24import android.provider.Settings; 25import android.util.Log; 26import android.util.Slog; 27import android.view.IWindowManager; 28import android.widget.CompoundButton; 29 30/** 31 * TODO: Listen for changes to the setting. 32 */ 33public class AutoRotateController implements CompoundButton.OnCheckedChangeListener { 34 private static final String TAG = "StatusBar.AutoRotateController"; 35 36 private Context mContext; 37 private CompoundButton mCheckBox; 38 39 private boolean mAutoRotation; 40 41 public AutoRotateController(Context context, CompoundButton checkbox) { 42 mContext = context; 43 mAutoRotation = getAutoRotation(); 44 mCheckBox = checkbox; 45 checkbox.setChecked(mAutoRotation); 46 checkbox.setOnCheckedChangeListener(this); 47 } 48 49 public void onCheckedChanged(CompoundButton view, boolean checked) { 50 if (checked != mAutoRotation) { 51 setAutoRotation(checked); 52 } 53 } 54 55 private boolean getAutoRotation() { 56 ContentResolver cr = mContext.getContentResolver(); 57 return 0 != Settings.System.getInt(cr, Settings.System.ACCELEROMETER_ROTATION, 0); 58 } 59 60 private void setAutoRotation(final boolean autorotate) { 61 mAutoRotation = autorotate; 62 AsyncTask.execute(new Runnable() { 63 public void run() { 64 try { 65 IWindowManager wm = IWindowManager.Stub.asInterface( 66 ServiceManager.getService(Context.WINDOW_SERVICE)); 67 if (autorotate) { 68 wm.thawRotation(); 69 } else { 70 wm.freezeRotation(-1); 71 } 72 } catch (RemoteException exc) { 73 Log.w(TAG, "Unable to save auto-rotate setting"); 74 } 75 } 76 }); 77 } 78} 79