1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
Copyright (c) 2021 The Linux Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of The Linux Foundation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.android.settings.development;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
import com.android.settings.R;
import com.qti.extphone.Client;
import com.qti.extphone.ExtPhoneCallbackBase;
import com.qti.extphone.ExtTelephonyManager;
import com.qti.extphone.ServiceCallback;
import com.qti.extphone.Token;
public class SmartDdsSwitchPreferenceController extends DeveloperOptionsPreferenceController
implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
private final String TAG = "SmartDdsSwitchPreferenceController";
private final int EVENT_SET_DEFAULT_TOGGLE_STATE_RESPONSE = 1;
private final int SETTING_VALUE_ON = 1;
private final int SETTING_VALUE_OFF = 0;
private static SmartDdsSwitchPreferenceController mInstance;
private Client mClient;
private Context mContext;
private String mPackageName;
private final TelephonyManager mTelephonyManager;
private ExtTelephonyManager mExtTelephonyManager;
private boolean mFeatureAvailable = false;
private boolean mServiceConnected = false;
private boolean mSwitchEnabled = false;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateState(mPreference);
}
};
private SmartDdsSwitchPreferenceController(Context context) {
super(context);
Log.d(TAG, "Constructor");
mContext = context.getApplicationContext();
mPackageName = mContext.getPackageName();
mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
mExtTelephonyManager = ExtTelephonyManager.getInstance(mContext);
mExtTelephonyManager.connectService(mExtTelManagerServiceCallback);
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
filter.addAction(TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED);
mContext.registerReceiver(mBroadcastReceiver, filter);
}
public static SmartDdsSwitchPreferenceController getInstance(Context context) {
if (mInstance == null) {
mInstance = new SmartDdsSwitchPreferenceController(context);
}
return mInstance;
}
public void cleanUp() {
Log.d(TAG, "Disconnecting ExtTelephonyService");
mExtTelephonyManager.disconnectService();
mInstance = null;
}
private ServiceCallback mExtTelManagerServiceCallback = new ServiceCallback() {
@Override
public void onConnected() {
Log.d(TAG, "ExtTelephonyService connected");
mServiceConnected = true;
mClient = mExtTelephonyManager.registerCallback(mPackageName, mCallback);
Log.d(TAG, "Client = " + mClient);
}
@Override
public void onDisconnected() {
Log.d(TAG, "ExtTelephonyService disconnected");
mContext.unregisterReceiver(mBroadcastReceiver);
mServiceConnected = false;
mClient = null;
}
};
private ExtPhoneCallbackBase mCallback = new ExtPhoneCallbackBase() {
@Override
public void setSmartDdsSwitchToggleResponse(Token token, boolean result) throws
RemoteException {
Log.d(TAG, "setSmartDdsSwitchToggleResponse: token = " + token + " result = " + result);
if (result) {
mHandler.sendMessage(mHandler.obtainMessage(
EVENT_SET_DEFAULT_TOGGLE_STATE_RESPONSE));
}
}
};
private Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case EVENT_SET_DEFAULT_TOGGLE_STATE_RESPONSE: {
Log.d(TAG, "EVENT_SET_DEFAULT_TOGGLE_STATE_RESPONSE");
String defaultSummary = mContext.getResources().getString(
R.string.smart_dds_switch_summary);
updateUi(defaultSummary, true);
putSwitchValue(mSwitchEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
break;
}
default:
Log.e(TAG, "Unsupported action");
}
}
};
@Override
public String getPreferenceKey() {
return Settings.Global.SMART_DDS_SWITCH;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mSwitchEnabled = (Boolean) newValue;
Log.d(TAG, "onPreferenceChange: isEnabled = " + mSwitchEnabled);
// Temporarily update the text and disable the button until the response is received
String waitSummary = mContext.getResources().getString(
R.string.smart_dds_switch_wait_summary);
updateUi(waitSummary, false);
if (mServiceConnected && mClient != null) {
try {
mExtTelephonyManager.setSmartDdsSwitchToggle(mSwitchEnabled, mClient);
return true;
} catch (Exception e) {
Log.e(TAG, "Exception " + e);
return false;
}
} else {
Log.e(TAG, "ExtTelephonyManager service not connected");
return false;
}
}
@Override
public void updateState(Preference preference) {
if (mPreference != null) {
final int smartDdsSwitch = getSwitchValue();
((SwitchPreference) mPreference).setChecked(smartDdsSwitch != SETTING_VALUE_OFF);
}
if (mServiceConnected) {
try {
mFeatureAvailable = mExtTelephonyManager.isSmartDdsSwitchFeatureAvailable();
} catch (Exception e) {
Log.e(TAG, "Exception " + e);
}
Log.d(TAG, "mFeatureAvailable: " + mFeatureAvailable);
if (mFeatureAvailable) {
String defaultSummary = mContext.getResources().getString(
R.string.smart_dds_switch_summary);
updateUi(defaultSummary, isAvailable());
} else {
Log.d(TAG, "Feature unavailable");
preference.setVisible(false);
}
} else {
Log.d(TAG, "Service not connected");
}
}
private void updateUi(String summary, boolean enable) {
Log.d(TAG, "updateUi enable: " + enable);
if (mPreference != null) {
((SwitchPreference) mPreference).setVisible(true);
((SwitchPreference) mPreference).setSummary(summary);
((SwitchPreference) mPreference).setEnabled(enable);
}
}
@Override
public boolean isAvailable() {
// Only show the toggle if 1) APM is off and 2) more than one subscription is active
SubscriptionManager subscriptionManager = mContext.getSystemService(
SubscriptionManager.class);
int numActiveSubscriptionInfoCount = subscriptionManager.getActiveSubscriptionInfoCount();
Log.d(TAG, "numActiveSubscriptionInfoCount: " + numActiveSubscriptionInfoCount);
return !isAirplaneModeOn() && (numActiveSubscriptionInfoCount > 1);
}
private boolean isAirplaneModeOn() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
private void putSwitchValue(int state) {
Settings.Global.putInt(mContext.getContentResolver(), getPreferenceKey(), state);
}
private int getSwitchValue() {
return Settings.Global.getInt(mContext.getContentResolver(), getPreferenceKey(),
SETTING_VALUE_OFF);
}
}
|