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 import javax.inject.Inject; 25 import javax.inject.Singleton; 26 27 /** 28 * Version of Space that can be resized by a tunable setting. 29 */ 30 public class TunablePadding implements Tunable { 31 32 public static final int FLAG_START = 1; 33 public static final int FLAG_END = 2; 34 public static final int FLAG_TOP = 4; 35 public static final int FLAG_BOTTOM = 8; 36 37 private final int mFlags; 38 private final View mView; 39 private final int mDefaultSize; 40 private final float mDensity; 41 private final TunerService mTunerService; 42 TunablePadding(String key, int def, int flags, View view, TunerService tunerService)43 private TunablePadding(String key, int def, int flags, View view, TunerService tunerService) { 44 mDefaultSize = def; 45 mFlags = flags; 46 mView = view; 47 DisplayMetrics metrics = new DisplayMetrics(); 48 view.getContext().getSystemService(WindowManager.class) 49 .getDefaultDisplay().getMetrics(metrics); 50 mDensity = metrics.density; 51 mTunerService = tunerService; 52 mTunerService.addTunable(this, key); 53 } 54 55 @Override onTuningChanged(String key, String newValue)56 public void onTuningChanged(String key, String newValue) { 57 int dimen = mDefaultSize; 58 if (newValue != null) { 59 try { 60 dimen = (int) (Integer.parseInt(newValue) * mDensity); 61 } catch (NumberFormatException ex) {} 62 } 63 int left = mView.isLayoutRtl() ? FLAG_END : FLAG_START; 64 int right = mView.isLayoutRtl() ? FLAG_START : FLAG_END; 65 mView.setPadding(getPadding(dimen, left), getPadding(dimen, FLAG_TOP), 66 getPadding(dimen, right), getPadding(dimen, FLAG_BOTTOM)); 67 } 68 getPadding(int dimen, int flag)69 private int getPadding(int dimen, int flag) { 70 return ((mFlags & flag) != 0) ? dimen : 0; 71 } 72 destroy()73 public void destroy() { 74 mTunerService.removeTunable(this); 75 } 76 77 /** 78 * Exists for easy injecting in tests. 79 */ 80 @Singleton 81 public static class TunablePaddingService { 82 83 private final TunerService mTunerService; 84 85 /** 86 */ 87 @Inject TunablePaddingService(TunerService tunerService)88 public TunablePaddingService(TunerService tunerService) { 89 mTunerService = tunerService; 90 } 91 add(View view, String key, int defaultSize, int flags)92 public TunablePadding add(View view, String key, int defaultSize, int flags) { 93 if (view == null) { 94 throw new IllegalArgumentException(); 95 } 96 return new TunablePadding(key, defaultSize, flags, view, mTunerService); 97 } 98 } 99 addTunablePadding(View view, String key, int defaultSize, int flags)100 public static TunablePadding addTunablePadding(View view, String key, int defaultSize, 101 int flags) { 102 return Dependency.get(TunablePaddingService.class).add(view, key, defaultSize, flags); 103 } 104 } 105