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.tuner;
16 
17 import android.content.Context;
18 import android.util.AttributeSet;
19 import android.view.MotionEvent;
20 
21 import com.android.systemui.statusbar.phone.NavigationBarInflaterView;
22 
23 public class PreviewNavInflater extends NavigationBarInflaterView {
24 
PreviewNavInflater(Context context, AttributeSet attrs)25     public PreviewNavInflater(Context context, AttributeSet attrs) {
26         super(context, attrs);
27     }
28 
29     @Override
onAttachedToWindow()30     protected void onAttachedToWindow() {
31         super.onAttachedToWindow();
32         // Immediately remove tuner listening, since this is a preview, all values will be injected
33         // manually.
34         TunerService.get(getContext()).removeTunable(this);
35     }
36 
37     @Override
onInterceptTouchEvent(MotionEvent ev)38     public boolean onInterceptTouchEvent(MotionEvent ev) {
39         // Only a preview, not interactable.
40         return true;
41     }
42 
43     @Override
onTuningChanged(String key, String newValue)44     public void onTuningChanged(String key, String newValue) {
45         if (NAV_BAR_VIEWS.equals(key)) {
46             // Since this is a preview we might get a bunch of random stuff, validate before sending
47             // for inflation.
48             if (isValidLayout(newValue)) {
49                 super.onTuningChanged(key, newValue);
50             }
51         } else {
52             super.onTuningChanged(key, newValue);
53         }
54     }
55 
isValidLayout(String newValue)56     private boolean isValidLayout(String newValue) {
57         if (newValue == null) {
58             return true;
59         }
60         int separatorCount = 0;
61         int lastGravitySeparator = 0;
62         for (int i = 0; i < newValue.length(); i++) {
63             if (newValue.charAt(i) == GRAVITY_SEPARATOR.charAt(0)) {
64                 if (i == 0 || (i - lastGravitySeparator) == 1) {
65                     return false;
66                 }
67                 lastGravitySeparator = i;
68                 separatorCount++;
69             }
70         }
71         return separatorCount == 2 && (newValue.length() - lastGravitySeparator) != 1;
72     }
73 }
74