1a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk/*
2a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk * Copyright (C) 2016 The Android Open Source Project
3a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk *
4a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk * except in compliance with the License. You may obtain a copy of the License at
6a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk *
7a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk *      http://www.apache.org/licenses/LICENSE-2.0
8a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk *
9a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk * Unless required by applicable law or agreed to in writing, software distributed under the
10a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk * KIND, either express or implied. See the License for the specific language governing
12a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk * permissions and limitations under the License.
13a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk */
14a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
15a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monkpackage com.android.systemui.tuner;
16a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
17a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monkimport android.content.Context;
18a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monkimport android.util.AttributeSet;
19a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monkimport android.view.MotionEvent;
20a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
21c0d7058b14c24cd07912f5629c26b39b7b4673d5Winsonimport com.android.systemui.statusbar.phone.NavigationBarInflaterView;
22c0d7058b14c24cd07912f5629c26b39b7b4673d5Winson
23a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monkpublic class PreviewNavInflater extends NavigationBarInflaterView {
24a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
25a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    public PreviewNavInflater(Context context, AttributeSet attrs) {
26a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        super(context, attrs);
27a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    }
28a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
29a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    @Override
30a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    protected void onAttachedToWindow() {
31a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        super.onAttachedToWindow();
32a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        // Immediately remove tuner listening, since this is a preview, all values will be injected
33a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        // manually.
34a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        TunerService.get(getContext()).removeTunable(this);
35a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    }
36a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
37a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    @Override
38a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    public boolean onInterceptTouchEvent(MotionEvent ev) {
39a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        // Only a preview, not interactable.
40a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        return true;
41a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    }
42a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
43a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    @Override
44a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    public void onTuningChanged(String key, String newValue) {
45a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        if (NAV_BAR_VIEWS.equals(key)) {
46a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            // Since this is a preview we might get a bunch of random stuff, validate before sending
47a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            // for inflation.
48a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            if (isValidLayout(newValue)) {
49a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk                super.onTuningChanged(key, newValue);
50a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            }
51a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        } else {
52a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            super.onTuningChanged(key, newValue);
53a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        }
54a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    }
55a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk
56a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    private boolean isValidLayout(String newValue) {
57a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        if (newValue == null) {
58a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            return true;
59a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        }
60a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        int separatorCount = 0;
61a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        int lastGravitySeparator = 0;
62a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        for (int i = 0; i < newValue.length(); i++) {
63a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            if (newValue.charAt(i) == GRAVITY_SEPARATOR.charAt(0)) {
64a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk                if (i == 0 || (i - lastGravitySeparator) == 1) {
65a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk                    return false;
66a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk                }
67a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk                lastGravitySeparator = i;
68a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk                separatorCount++;
69a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk            }
70a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        }
71a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk        return separatorCount == 2 && (newValue.length() - lastGravitySeparator) != 1;
72a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk    }
73a9f128832f3fc16c2f435f2d0c999a5154200b4cJason Monk}
74