1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.volume;
18 
19 import android.media.AudioManager;
20 import android.util.MathUtils;
21 import android.view.View;
22 
23 /**
24  * Static helpers for the volume dialog.
25  */
26 class Util extends com.android.settingslib.volume.Util {
27 
logTag(Class<?> c)28     public static String logTag(Class<?> c) {
29         final String tag = "vol." + c.getSimpleName();
30         return tag.length() < 23 ? tag : tag.substring(0, 23);
31     }
32 
ringerModeToString(int ringerMode)33     public static String ringerModeToString(int ringerMode) {
34         switch (ringerMode) {
35             case AudioManager.RINGER_MODE_SILENT:
36                 return "RINGER_MODE_SILENT";
37             case AudioManager.RINGER_MODE_VIBRATE:
38                 return "RINGER_MODE_VIBRATE";
39             case AudioManager.RINGER_MODE_NORMAL:
40                 return "RINGER_MODE_NORMAL";
41             default:
42                 return "RINGER_MODE_UNKNOWN_" + ringerMode;
43         }
44     }
45 
setVisOrGone(View v, boolean vis)46     public static final void setVisOrGone(View v, boolean vis) {
47         if (v == null || (v.getVisibility() == View.VISIBLE) == vis) return;
48         v.setVisibility(vis ? View.VISIBLE : View.GONE);
49     }
50 
51     /**
52      * Translates a value from one range to another.
53      *
54      * ```
55      * Given: currentValue=3, currentRange=[0, 8], targetRange=[0, 100]
56      * Result: 37.5
57      * ```
58      */
translateToRange(float value, float valueRangeStart, float valueRangeEnd, float targetRangeStart, float targetRangeEnd)59     public static float translateToRange(float value,
60             float valueRangeStart,
61             float valueRangeEnd,
62             float targetRangeStart,
63             float targetRangeEnd) {
64         float currentRangeLength = valueRangeEnd - valueRangeStart;
65         float targetRangeLength = targetRangeEnd - targetRangeStart;
66         if (currentRangeLength == 0f || targetRangeLength == 0f) {
67             return targetRangeStart;
68         }
69         float valueFraction = (value - valueRangeStart) / currentRangeLength;
70         return MathUtils.constrain(targetRangeStart + valueFraction * targetRangeLength,
71                 targetRangeStart, targetRangeEnd);
72     }
73 }
74