1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui;
16 
17 import android.content.Context;
18 import android.content.res.Configuration;
19 import android.content.res.TypedArray;
20 import android.graphics.Canvas;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.view.ViewGroup.LayoutParams;
24 
25 public class ResizingSpace extends View {
26 
27     private final int mWidth;
28     private final int mHeight;
29 
ResizingSpace(Context context, AttributeSet attrs)30     public ResizingSpace(Context context, AttributeSet attrs) {
31         super(context, attrs);
32         if (getVisibility() == VISIBLE) {
33             setVisibility(INVISIBLE);
34         }
35         TypedArray a = context.obtainStyledAttributes(attrs, android.R.styleable.ViewGroup_Layout);
36         mWidth = a.getResourceId(android.R.styleable.ViewGroup_Layout_layout_width, 0);
37         mHeight = a.getResourceId(android.R.styleable.ViewGroup_Layout_layout_height, 0);
38     }
39 
40     @Override
onConfigurationChanged(Configuration newConfig)41     protected void onConfigurationChanged(Configuration newConfig) {
42         super.onConfigurationChanged(newConfig);
43         LayoutParams params = getLayoutParams();
44         boolean changed = false;
45         if (mWidth > 0) {
46             int width = getContext().getResources().getDimensionPixelOffset(mWidth);
47             if (width != params.width) {
48                 params.width = width;
49                 changed = true;
50             }
51         }
52         if (mHeight > 0) {
53             int height = getContext().getResources().getDimensionPixelOffset(mHeight);
54             if (height != params.height) {
55                 params.height = height;
56                 changed = true;
57             }
58         }
59         if (changed) {
60             setLayoutParams(params);
61         }
62     }
63 
64     /**
65      * Draw nothing.
66      *
67      * @param canvas an unused parameter.
68      */
69     @Override
draw(Canvas canvas)70     public void draw(Canvas canvas) {
71     }
72 
73     /**
74      * Compare to: {@link View#getDefaultSize(int, int)}
75      * If mode is AT_MOST, return the child size instead of the parent size
76      * (unless it is too big).
77      */
getDefaultSize2(int size, int measureSpec)78     private static int getDefaultSize2(int size, int measureSpec) {
79         int result = size;
80         int specMode = MeasureSpec.getMode(measureSpec);
81         int specSize = MeasureSpec.getSize(measureSpec);
82 
83         switch (specMode) {
84             case MeasureSpec.UNSPECIFIED:
85                 result = size;
86                 break;
87             case MeasureSpec.AT_MOST:
88                 result = Math.min(size, specSize);
89                 break;
90             case MeasureSpec.EXACTLY:
91                 result = specSize;
92                 break;
93         }
94         return result;
95     }
96 
97     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)98     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
99         setMeasuredDimension(
100                 getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
101                 getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));
102     }
103 
104 }
105