• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         a.recycle();
39     }
40 
41     @Override
onConfigurationChanged(Configuration newConfig)42     protected void onConfigurationChanged(Configuration newConfig) {
43         super.onConfigurationChanged(newConfig);
44         LayoutParams params = getLayoutParams();
45         boolean changed = false;
46         if (mWidth > 0) {
47             int width = getContext().getResources().getDimensionPixelOffset(mWidth);
48             if (width != params.width) {
49                 params.width = width;
50                 changed = true;
51             }
52         }
53         if (mHeight > 0) {
54             int height = getContext().getResources().getDimensionPixelOffset(mHeight);
55             if (height != params.height) {
56                 params.height = height;
57                 changed = true;
58             }
59         }
60         if (changed) {
61             setLayoutParams(params);
62         }
63     }
64 
65     /**
66      * Draw nothing.
67      *
68      * @param canvas an unused parameter.
69      */
70     @Override
draw(Canvas canvas)71     public void draw(Canvas canvas) {
72     }
73 
74     /**
75      * Compare to: {@link View#getDefaultSize(int, int)}
76      * If mode is AT_MOST, return the child size instead of the parent size
77      * (unless it is too big).
78      */
getDefaultSize2(int size, int measureSpec)79     private static int getDefaultSize2(int size, int measureSpec) {
80         int result = size;
81         int specMode = MeasureSpec.getMode(measureSpec);
82         int specSize = MeasureSpec.getSize(measureSpec);
83 
84         switch (specMode) {
85             case MeasureSpec.UNSPECIFIED:
86                 result = size;
87                 break;
88             case MeasureSpec.AT_MOST:
89                 result = Math.min(size, specSize);
90                 break;
91             case MeasureSpec.EXACTLY:
92                 result = specSize;
93                 break;
94         }
95         return result;
96     }
97 
98     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)99     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
100         setMeasuredDimension(
101                 getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
102                 getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));
103     }
104 
105 }
106