You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.2 KiB
125 lines
4.2 KiB
/*
|
|
* Copyright (C) 2019 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.display;
|
|
|
|
import android.content.Context;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.ServiceManager;
|
|
import android.provider.Settings;
|
|
import android.util.Log;
|
|
import android.view.IWindowManager;
|
|
|
|
import androidx.preference.ListPreference;
|
|
import androidx.preference.Preference;
|
|
|
|
import com.android.settings.R;
|
|
import com.android.settings.core.BasePreferenceController;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class LockUserRotationPreferenceController extends BasePreferenceController implements
|
|
Preference.OnPreferenceChangeListener {
|
|
|
|
private static final String TAG = "LockRotation";
|
|
|
|
private final Map<String, String> mRotationValueToTitleMap;
|
|
private int mUserRotation;
|
|
|
|
private Handler mHandler;
|
|
private IWindowManager mWindowManager;
|
|
|
|
public LockUserRotationPreferenceController(Context context, String preferenceKey) {
|
|
super(context, preferenceKey);
|
|
|
|
mHandler = new Handler(Looper.getMainLooper());
|
|
mWindowManager = IWindowManager.Stub.asInterface(
|
|
ServiceManager.getService(Context.WINDOW_SERVICE));
|
|
|
|
mRotationValueToTitleMap = new HashMap<>();
|
|
initDisplayRotation();
|
|
}
|
|
|
|
@Override
|
|
public int getAvailabilityStatus() {
|
|
return AVAILABLE;
|
|
}
|
|
|
|
@Override
|
|
public boolean onPreferenceChange(Preference preference, Object object) {
|
|
if (!(preference instanceof ListPreference)) {
|
|
return false;
|
|
}
|
|
final ListPreference listPreference = (ListPreference) preference;
|
|
mUserRotation = Integer.parseInt((String) object);
|
|
updateState(listPreference);
|
|
|
|
Log.d(TAG, "freeze Rotation to " + mUserRotation);
|
|
mHandler.postDelayed(() -> {
|
|
try {
|
|
mWindowManager.freezeRotation(mUserRotation);
|
|
} catch (Exception e) {
|
|
Log.e(TAG, "Failed to set user rotation", e);
|
|
}
|
|
}, 150);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void updateState(Preference preference) {
|
|
super.updateState(preference);
|
|
if (!(preference instanceof ListPreference)) {
|
|
return;
|
|
}
|
|
final ListPreference listPreference = (ListPreference) preference;
|
|
listPreference.setValue(getUserRotationValue());
|
|
}
|
|
|
|
@Override
|
|
public CharSequence getSummary() {
|
|
return mRotationValueToTitleMap.get(getUserRotationValue());
|
|
}
|
|
|
|
private String getUserRotationValue() {
|
|
return String.valueOf(mUserRotation);
|
|
}
|
|
|
|
private void initDisplayRotation() {
|
|
if (mRotationValueToTitleMap.size() == 0) {
|
|
final String[] rotationValues = mContext.getResources().getStringArray(
|
|
R.array.user_rotation_values);
|
|
final String[] rotationTitles = mContext.getResources().getStringArray(
|
|
R.array.user_rotation_entries);
|
|
final int rotationValueCount = rotationValues.length;
|
|
|
|
mRotationValueToTitleMap.put(rotationValues[0], "natural orientation");
|
|
for (int i = 1; i < rotationValueCount; i++) {
|
|
mRotationValueToTitleMap.put(rotationValues[i],
|
|
"clockwise " + rotationTitles[i] + "-degree");
|
|
}
|
|
|
|
try {
|
|
mUserRotation = mWindowManager.getDefaultDisplayRotation();
|
|
} catch (Exception e) {
|
|
Log.e(TAG, "Failed to get default rotation", e);
|
|
mUserRotation = Integer.parseInt(rotationValues[0]);
|
|
}
|
|
}
|
|
}
|
|
}
|