Home | History | Annotate | Download | only in volume
      1 /*
      2  * Copyright (C) 2019 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.settingslib.volume;
     18 
     19 import android.content.Context;
     20 import android.media.AudioManager;
     21 import android.media.MediaMetadata;
     22 import android.media.VolumeProvider;
     23 import android.media.session.MediaController.PlaybackInfo;
     24 import android.media.session.PlaybackState;
     25 import android.telephony.TelephonyManager;
     26 import android.widget.TextView;
     27 
     28 import java.util.Objects;
     29 
     30 /**
     31  * Static helpers for the volume dialog.
     32  */
     33 public class Util {
     34 
     35     private static final int[] AUDIO_MANAGER_FLAGS = new int[]{
     36             AudioManager.FLAG_SHOW_UI,
     37             AudioManager.FLAG_VIBRATE,
     38             AudioManager.FLAG_PLAY_SOUND,
     39             AudioManager.FLAG_ALLOW_RINGER_MODES,
     40             AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE,
     41             AudioManager.FLAG_SHOW_VIBRATE_HINT,
     42             AudioManager.FLAG_SHOW_SILENT_HINT,
     43             AudioManager.FLAG_FROM_KEY,
     44             AudioManager.FLAG_SHOW_UI_WARNINGS,
     45     };
     46 
     47     private static final String[] AUDIO_MANAGER_FLAG_NAMES = new String[]{
     48             "SHOW_UI",
     49             "VIBRATE",
     50             "PLAY_SOUND",
     51             "ALLOW_RINGER_MODES",
     52             "REMOVE_SOUND_AND_VIBRATE",
     53             "SHOW_VIBRATE_HINT",
     54             "SHOW_SILENT_HINT",
     55             "FROM_KEY",
     56             "SHOW_UI_WARNINGS",
     57     };
     58 
     59     /**
     60      * Extract log tag from {@code c}
     61      */
     62     public static String logTag(Class<?> c) {
     63         final String tag = "vol." + c.getSimpleName();
     64         return tag.length() < 23 ? tag : tag.substring(0, 23);
     65     }
     66 
     67     /**
     68      * Convert media metadata to string
     69      */
     70     public static String mediaMetadataToString(MediaMetadata metadata) {
     71         if (metadata == null) return null;
     72         return metadata.getDescription().toString();
     73     }
     74 
     75     /**
     76      * Convert playback info to string
     77      */
     78     public static String playbackInfoToString(PlaybackInfo info) {
     79         if (info == null) return null;
     80         final String type = playbackInfoTypeToString(info.getPlaybackType());
     81         final String vc = volumeProviderControlToString(info.getVolumeControl());
     82         return String.format("PlaybackInfo[vol=%s,max=%s,type=%s,vc=%s],atts=%s",
     83                 info.getCurrentVolume(), info.getMaxVolume(), type, vc, info.getAudioAttributes());
     84     }
     85 
     86     /**
     87      * Convert type of playback info to string
     88      */
     89     public static String playbackInfoTypeToString(int type) {
     90         switch (type) {
     91             case PlaybackInfo.PLAYBACK_TYPE_LOCAL:
     92                 return "LOCAL";
     93             case PlaybackInfo.PLAYBACK_TYPE_REMOTE:
     94                 return "REMOTE";
     95             default:
     96                 return "UNKNOWN_" + type;
     97         }
     98     }
     99 
    100     /**
    101      * Convert state of playback info to string
    102      */
    103     public static String playbackStateStateToString(int state) {
    104         switch (state) {
    105             case PlaybackState.STATE_NONE:
    106                 return "STATE_NONE";
    107             case PlaybackState.STATE_STOPPED:
    108                 return "STATE_STOPPED";
    109             case PlaybackState.STATE_PAUSED:
    110                 return "STATE_PAUSED";
    111             case PlaybackState.STATE_PLAYING:
    112                 return "STATE_PLAYING";
    113             default:
    114                 return "UNKNOWN_" + state;
    115         }
    116     }
    117 
    118     /**
    119      * Convert volume provider control to string
    120      */
    121     public static String volumeProviderControlToString(int control) {
    122         switch (control) {
    123             case VolumeProvider.VOLUME_CONTROL_ABSOLUTE:
    124                 return "VOLUME_CONTROL_ABSOLUTE";
    125             case VolumeProvider.VOLUME_CONTROL_FIXED:
    126                 return "VOLUME_CONTROL_FIXED";
    127             case VolumeProvider.VOLUME_CONTROL_RELATIVE:
    128                 return "VOLUME_CONTROL_RELATIVE";
    129             default:
    130                 return "VOLUME_CONTROL_UNKNOWN_" + control;
    131         }
    132     }
    133 
    134     /**
    135      * Convert {@link PlaybackState} to string
    136      */
    137     public static String playbackStateToString(PlaybackState playbackState) {
    138         if (playbackState == null) return null;
    139         return playbackStateStateToString(playbackState.getState()) + " " + playbackState;
    140     }
    141 
    142     /**
    143      * Convert audio manager flags to string
    144      */
    145     public static String audioManagerFlagsToString(int value) {
    146         return bitFieldToString(value, AUDIO_MANAGER_FLAGS, AUDIO_MANAGER_FLAG_NAMES);
    147     }
    148 
    149     protected static String bitFieldToString(int value, int[] values, String[] names) {
    150         if (value == 0) return "";
    151         final StringBuilder sb = new StringBuilder();
    152         for (int i = 0; i < values.length; i++) {
    153             if ((value & values[i]) != 0) {
    154                 if (sb.length() > 0) sb.append(',');
    155                 sb.append(names[i]);
    156             }
    157             value &= ~values[i];
    158         }
    159         if (value != 0) {
    160             if (sb.length() > 0) sb.append(',');
    161             sb.append("UNKNOWN_").append(value);
    162         }
    163         return sb.toString();
    164     }
    165 
    166     private static CharSequence emptyToNull(CharSequence str) {
    167         return str == null || str.length() == 0 ? null : str;
    168     }
    169 
    170     /**
    171      * Set text for specific {@link TextView}
    172      */
    173     public static boolean setText(TextView tv, CharSequence text) {
    174         if (Objects.equals(emptyToNull(tv.getText()), emptyToNull(text))) return false;
    175         tv.setText(text);
    176         return true;
    177     }
    178 
    179     /**
    180      * Return {@code true} if it is voice capable
    181      */
    182     public static boolean isVoiceCapable(Context context) {
    183         final TelephonyManager telephony =
    184                 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    185         return telephony != null && telephony.isVoiceCapable();
    186     }
    187 }
    188