Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d24c2046 authored by Roman Birg's avatar Roman Birg
Browse files

Settings: use WindowManager to set custom DPI



Change-Id: I719bf391cc8746466c466b4b5a9a0e2bf9fc0056
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent bc8f2d9b
Loading
Loading
Loading
Loading
+41 −10
Original line number Diff line number Diff line
@@ -19,6 +19,12 @@ package com.android.settings;

import android.os.UserHandle;

import android.view.Display;
import android.view.IWindowManager;
import android.view.WindowManager;
import android.view.WindowManagerGlobal;
import android.view.WindowManagerImpl;
import android.widget.Toast;
import com.android.internal.view.RotationPolicy;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
@@ -172,8 +178,8 @@ public class DisplaySettings extends SettingsPreferenceFragment implements

        mLcdDensityPreference = (ListPreference) findPreference(KEY_LCD_DENSITY);
        if (mLcdDensityPreference != null) {
            int defaultDensity = DisplayMetrics.DENSITY_DEVICE;
            int currentDensity = DisplayMetrics.DENSITY_CURRENT;
            int defaultDensity = getDefaultDensity();
            int currentDensity = getCurrentDensity();
            if (currentDensity < 10 || currentDensity >= 1000) {
                // Unsupported value, force default
                currentDensity = defaultDensity;
@@ -259,6 +265,28 @@ public class DisplaySettings extends SettingsPreferenceFragment implements
        initPulse((PreferenceCategory) findPreference(KEY_CATEGORY_LIGHTS));
    }

    private int getDefaultDensity() {
        IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.checkService(
                Context.WINDOW_SERVICE));
        try {
            return wm.getInitialDisplayDensity(Display.DEFAULT_DISPLAY);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return DisplayMetrics.DENSITY_DEVICE;
    }

    private int getCurrentDensity() {
        IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.checkService(
                Context.WINDOW_SERVICE));
        try {
            return wm.getBaseDisplayDensity(Display.DEFAULT_DISPLAY);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return DisplayMetrics.DENSITY_DEVICE;
    }

    private static boolean allowAllRotations(Context context) {
        return Resources.getSystem().getBoolean(
                com.android.internal.R.bool.config_allowAllRotations);
@@ -354,7 +382,7 @@ public class DisplaySettings extends SettingsPreferenceFragment implements
    }

    private void updateLcdDensityPreferenceDescription(int currentDensity) {
        final int summaryResId = currentDensity == DisplayMetrics.DENSITY_DEVICE
        final int summaryResId = currentDensity == getDefaultDensity()
                ? R.string.lcd_density_default_value_format : R.string.lcd_density_value_format;
        mLcdDensityPreference.setSummary(getString(summaryResId, currentDensity));
    }
@@ -489,15 +517,11 @@ public class DisplaySettings extends SettingsPreferenceFragment implements
        }
    }

    private void writeLcdDensityPreference(final Context context, int value) {
        try {
            SystemProperties.set("persist.sys.lcd_density", Integer.toString(value));
        } catch (RuntimeException e) {
            Log.e(TAG, "Unable to save LCD density");
            return;
        }
    private void writeLcdDensityPreference(final Context context, final int density) {
        final IActivityManager am = ActivityManagerNative.asInterface(
                ServiceManager.checkService("activity"));
        final IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.checkService(
                Context.WINDOW_SERVICE));
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
            @Override
            protected void onPreExecute() {
@@ -515,6 +539,13 @@ public class DisplaySettings extends SettingsPreferenceFragment implements
                } catch (InterruptedException e) {
                    // Ignore
                }

                try {
                    wm.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, density);
                } catch (RemoteException e) {
                    Log.e(TAG, "Failed to set density to " + density, e);
                }

                // Restart the UI
                try {
                    am.restart();