1 /* 2 * Copyright (C) 2017 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.util.DisplayMetrics; 18 import android.view.View; 19 import android.view.WindowManager; 20 21 import com.android.systemui.Dependency; 22 import com.android.systemui.tuner.TunerService.Tunable; 23 24 /** 25 * Version of Space that can be resized by a tunable setting. 26 */ 27 public class TunablePadding implements Tunable { 28 29 public static final int FLAG_START = 1; 30 public static final int FLAG_END = 2; 31 public static final int FLAG_TOP = 4; 32 public static final int FLAG_BOTTOM = 8; 33 34 private final int mFlags; 35 private final View mView; 36 private final int mDefaultSize; 37 private final float mDensity; 38 39 private TunablePadding(String key, int def, int flags, View view) { 40 mDefaultSize = def; 41 mFlags = flags; 42 mView = view; 43 DisplayMetrics metrics = new DisplayMetrics(); 44 view.getContext().getSystemService(WindowManager.class) 45 .getDefaultDisplay().getMetrics(metrics); 46 mDensity = metrics.density; 47 Dependency.get(TunerService.class).addTunable(this, key); 48 } 49 50 @Override 51 public void onTuningChanged(String key, String newValue) { 52 int dimen = mDefaultSize; 53 if (newValue != null) { 54 dimen = (int) (Integer.parseInt(newValue) * mDensity); 55 } 56 int left = mView.isLayoutRtl() ? FLAG_END : FLAG_START; 57 int right = mView.isLayoutRtl() ? FLAG_START : FLAG_END; 58 mView.setPadding(getPadding(dimen, left), getPadding(dimen, FLAG_TOP), 59 getPadding(dimen, right), getPadding(dimen, FLAG_BOTTOM)); 60 } 61 62 private int getPadding(int dimen, int flag) { 63 return ((mFlags & flag) != 0) ? dimen : 0; 64 } 65 66 public void destroy() { 67 Dependency.get(TunerService.class).removeTunable(this); 68 } 69 70 // Exists for easy injecting in tests. 71 public static class TunablePaddingService { 72 public TunablePadding add(View view, String key, int defaultSize, int flags) { 73 if (view == null) { 74 throw new IllegalArgumentException(); 75 } 76 return new TunablePadding(key, defaultSize, flags, view); 77 } 78 } 79 80 public static TunablePadding addTunablePadding(View view, String key, int defaultSize, 81 int flags) { 82 return Dependency.get(TunablePaddingService.class).add(view, key, defaultSize, flags); 83 } 84 } 85