1 /*
2  * Copyright (C) 2017 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.settings.notification;
18 
19 import static com.android.settings.notification.SettingPref.TYPE_GLOBAL;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.res.Resources;
25 import android.provider.Settings.Global;
26 
27 import com.android.settings.SettingsPreferenceFragment;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 public class DockAudioMediaPreferenceController extends SettingPrefController {
31 
32     private static final String KEY_DOCK_AUDIO_MEDIA = "dock_audio_media";
33 
34     private static final int DOCK_AUDIO_MEDIA_DISABLED = 0;
35     private static final int DOCK_AUDIO_MEDIA_ENABLED = 1;
36     private static final int DEFAULT_DOCK_AUDIO_MEDIA = DOCK_AUDIO_MEDIA_DISABLED;
37 
DockAudioMediaPreferenceController(Context context, SettingsPreferenceFragment parent, Lifecycle lifecycle)38     public DockAudioMediaPreferenceController(Context context, SettingsPreferenceFragment parent,
39             Lifecycle lifecycle) {
40         super(context, parent, lifecycle);
41         mPreference = new SettingPref(
42             TYPE_GLOBAL, KEY_DOCK_AUDIO_MEDIA, Global.DOCK_AUDIO_MEDIA_ENABLED,
43             DEFAULT_DOCK_AUDIO_MEDIA, DOCK_AUDIO_MEDIA_DISABLED, DOCK_AUDIO_MEDIA_ENABLED) {
44             @Override
45             public boolean isApplicable(Context context) {
46                 return isLeDesk() && context.getResources().getBoolean(
47                     com.android.settings.R.bool.has_dock_settings);
48             }
49 
50             @Override
51             protected String getCaption(Resources res, int value) {
52                 switch(value) {
53                     case DOCK_AUDIO_MEDIA_DISABLED:
54                         return res.getString(
55                             com.android.settings.R.string.dock_audio_media_disabled);
56                     case DOCK_AUDIO_MEDIA_ENABLED:
57                         return res.getString(
58                             com.android.settings.R.string.dock_audio_media_enabled);
59                     default:
60                         throw new IllegalArgumentException();
61                 }
62             }
63         };
64     }
65 
66     /**
67      * Checks the state of docking type
68      * @return true if it is low-end dock types
69      */
isLeDesk()70     private boolean isLeDesk() {
71         IntentFilter intentFilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
72         Intent dockStatus = mContext.registerReceiver(null, intentFilter);
73         if (dockStatus == null) {
74             return false;
75         }
76         int dockState = dockStatus.getIntExtra(Intent.EXTRA_DOCK_STATE, -1);
77         return dockState == Intent.EXTRA_DOCK_STATE_LE_DESK;
78     }
79 }
80