RecentsConfiguration.java revision 303e1ff1fec8b240b587bb18b981247a99833aa8
1/* 2 * Copyright (C) 2014 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.recents; 18 19import android.content.Context; 20import android.content.res.Configuration; 21import android.graphics.Rect; 22import android.util.DisplayMetrics; 23import android.util.TypedValue; 24 25 26/** A static Recents configuration for the current context 27 * NOTE: We should not hold any references to a Context from a static instance */ 28public class RecentsConfiguration { 29 static RecentsConfiguration sInstance; 30 31 DisplayMetrics mDisplayMetrics; 32 33 public boolean layoutVerticalStack; 34 public Rect systemInsets = new Rect(); 35 36 /** Private constructor */ 37 private RecentsConfiguration() {} 38 39 /** Updates the configuration to the current context */ 40 public static RecentsConfiguration reinitialize(Context context) { 41 if (sInstance == null) { 42 sInstance = new RecentsConfiguration(); 43 } 44 sInstance.update(context); 45 return sInstance; 46 } 47 48 /** Returns the current recents configuration */ 49 public static RecentsConfiguration getInstance() { 50 return sInstance; 51 } 52 53 /** Updates the state, given the specified context */ 54 void update(Context context) { 55 mDisplayMetrics = context.getResources().getDisplayMetrics(); 56 57 boolean isPortrait = context.getResources().getConfiguration().orientation == 58 Configuration.ORIENTATION_PORTRAIT; 59 layoutVerticalStack = isPortrait || Constants.LANDSCAPE_LAYOUT_VERTICAL_STACK; 60 } 61 62 public void updateSystemInsets(Rect insets) { 63 systemInsets.set(insets); 64 } 65 66 /** Converts from DPs to PXs */ 67 public int pxFromDp(float size) { 68 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 69 size, mDisplayMetrics)); 70 } 71 /** Converts from SPs to PXs */ 72 public int pxFromSp(float size) { 73 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 74 size, mDisplayMetrics)); 75 } 76} 77