RecentsConfiguration.java revision 4d7b092a866d2fce3e11b5a12cda2b87a83af52d
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 Rect systemInsets = new Rect(); 34 35 /** Private constructor */ 36 private RecentsConfiguration() {} 37 38 /** Updates the configuration to the current context */ 39 public static RecentsConfiguration reinitialize(Context context) { 40 if (sInstance == null) { 41 sInstance = new RecentsConfiguration(); 42 } 43 sInstance.update(context); 44 return sInstance; 45 } 46 47 /** Returns the current recents configuration */ 48 public static RecentsConfiguration getInstance() { 49 return sInstance; 50 } 51 52 /** Updates the state, given the specified context */ 53 void update(Context context) { 54 mDisplayMetrics = context.getResources().getDisplayMetrics(); 55 56 boolean isPortrait = context.getResources().getConfiguration().orientation == 57 Configuration.ORIENTATION_PORTRAIT; 58 } 59 60 public void updateSystemInsets(Rect insets) { 61 systemInsets.set(insets); 62 } 63 64 /** Converts from DPs to PXs */ 65 public int pxFromDp(float size) { 66 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 67 size, mDisplayMetrics)); 68 } 69 /** Converts from SPs to PXs */ 70 public int pxFromSp(float size) { 71 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 72 size, mDisplayMetrics)); 73 } 74} 75