1 /*
2  * Copyright (C) 2017 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 package com.android.wallpaper.util;
17 
18 import android.content.res.Configuration;
19 import android.content.res.Resources;
20 import android.util.DisplayMetrics;
21 import android.util.Log;
22 import android.view.Display;
23 
24 /**
25  * Retrieves copies of DisplayMetrics from Display objects and caches them in memory. All clients
26  * that need a copy of DisplayMetrics should use this retriever class to avoid excessive binder
27  * calls to the system server.
28  */
29 public class DisplayMetricsRetriever {
30 
31     private static final String TAG = "DisplayMetricsRetriever";
32 
33     private static DisplayMetricsRetriever sInstance;
34 
35     private DisplayMetrics mPortraitDisplayMetrics;
36     private DisplayMetrics mLandscapeDisplayMetrics;
37 
getInstance()38     public static DisplayMetricsRetriever getInstance() {
39         if (sInstance == null) {
40             sInstance = new DisplayMetricsRetriever();
41         }
42         return sInstance;
43     }
44 
45     /**
46      * Clears the static instance of DisplayMetricsRetriever. Used in test when display metrics are
47      * manipulated between test cases.
48      */
clearInstance()49     static void clearInstance() {
50         sInstance = null;
51     }
52 
53     /**
54      * Returns the DisplayMetrics for the provided Display.
55      */
getDisplayMetrics(Resources resources, Display display)56     public DisplayMetrics getDisplayMetrics(Resources resources, Display display) {
57         switch (resources.getConfiguration().orientation) {
58             case Configuration.ORIENTATION_PORTRAIT:
59                 return getPortraitDisplayMetrics(display);
60             case Configuration.ORIENTATION_LANDSCAPE:
61                 return getLandscapeDisplayMetrics(display);
62             default:
63                 Log.e(TAG, "Unknown device orientation: " + resources.getConfiguration().orientation);
64                 return getPortraitDisplayMetrics(display);
65         }
66     }
67 
getPortraitDisplayMetrics(Display display)68     private DisplayMetrics getPortraitDisplayMetrics(Display display) {
69         if (mPortraitDisplayMetrics == null) {
70             mPortraitDisplayMetrics = new DisplayMetrics();
71             writeDisplayMetrics(display, mPortraitDisplayMetrics);
72         }
73         return mPortraitDisplayMetrics;
74     }
75 
getLandscapeDisplayMetrics(Display display)76     private DisplayMetrics getLandscapeDisplayMetrics(Display display) {
77         if (mLandscapeDisplayMetrics == null) {
78             mLandscapeDisplayMetrics = new DisplayMetrics();
79             writeDisplayMetrics(display, mLandscapeDisplayMetrics);
80         }
81         return mLandscapeDisplayMetrics;
82     }
83 
writeDisplayMetrics(Display display, DisplayMetrics outMetrics)84     private void writeDisplayMetrics(Display display, DisplayMetrics outMetrics) {
85         display.getMetrics(outMetrics);
86     }
87 }
88