1 /* 2 * Copyright (C) 2020 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.sim.smartForwarding; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.telephony.CallForwardingInfo; 22 import android.telephony.SubscriptionInfo; 23 import android.telephony.SubscriptionManager; 24 import android.telephony.TelephonyManager; 25 26 public class SmartForwardingUtils { 27 public static final String TAG = "SmartForwarding"; 28 public static final String SMART_FORWARDING_PREF = "smart_forwarding_pref_"; 29 30 public static final String CALL_WAITING_KEY = "call_waiting_key"; 31 public static final String CALL_FORWARDING_ENABLED_KEY = "call_forwarding_enabled_key"; 32 public static final String CALL_FORWARDING_REASON_KEY = "call_forwarding_reason_key"; 33 public static final String CALL_FORWARDING_NUMBER_KEY = "call_forwarding_number_key"; 34 public static final String CALL_FORWARDING_TIME_KEY = "call_forwarding_timekey"; 35 getBackupCallWaitingStatus(Context context, int subId)36 public static boolean getBackupCallWaitingStatus(Context context, int subId) { 37 SharedPreferences preferences = context.getSharedPreferences( 38 SMART_FORWARDING_PREF + subId, Context.MODE_PRIVATE); 39 return preferences.getBoolean(CALL_WAITING_KEY, false); 40 } 41 getBackupCallForwardingStatus(Context context, int subId)42 public static CallForwardingInfo getBackupCallForwardingStatus(Context context, int subId) { 43 SharedPreferences preferences = context.getSharedPreferences( 44 SMART_FORWARDING_PREF + subId, Context.MODE_PRIVATE); 45 if (preferences.contains(CALL_FORWARDING_ENABLED_KEY)) { 46 boolean enabled = preferences.getBoolean(CALL_FORWARDING_ENABLED_KEY, false); 47 int reason = preferences.getInt(CALL_FORWARDING_REASON_KEY, 48 CallForwardingInfo.REASON_UNCONDITIONAL); 49 String number = preferences.getString(CALL_FORWARDING_NUMBER_KEY, ""); 50 int time = preferences.getInt(CALL_FORWARDING_TIME_KEY, 1); 51 52 return new CallForwardingInfo(enabled, reason, number, time); 53 } else { 54 return null; 55 } 56 } 57 saveCallWaitingStatus(Context context, int subId, boolean value)58 public static void saveCallWaitingStatus(Context context, int subId, boolean value) { 59 SharedPreferences.Editor preferences = context.getSharedPreferences( 60 SMART_FORWARDING_PREF + subId, Context.MODE_PRIVATE).edit(); 61 preferences.putBoolean(CALL_WAITING_KEY, value).commit(); 62 } 63 saveCallForwardingStatus(Context context, int subId, CallForwardingInfo callForwardingInfo)64 public static void saveCallForwardingStatus(Context context, int subId, 65 CallForwardingInfo callForwardingInfo) { 66 SharedPreferences.Editor preferences = context.getSharedPreferences( 67 SMART_FORWARDING_PREF + subId, Context.MODE_PRIVATE).edit(); 68 69 preferences.putBoolean(CALL_FORWARDING_ENABLED_KEY, callForwardingInfo.isEnabled()) 70 .commit(); 71 preferences.putInt(CALL_FORWARDING_REASON_KEY, callForwardingInfo.getReason()).commit(); 72 preferences.putString(CALL_FORWARDING_NUMBER_KEY, callForwardingInfo.getNumber()).commit(); 73 preferences.putInt(CALL_FORWARDING_TIME_KEY, callForwardingInfo.getTimeoutSeconds()) 74 .commit(); 75 } 76 clearBackupData(Context context, int subId)77 public static void clearBackupData(Context context, int subId) { 78 SharedPreferences.Editor preferences = context.getSharedPreferences( 79 SMART_FORWARDING_PREF + subId, Context.MODE_PRIVATE).edit(); 80 preferences.clear().commit(); 81 } 82 getAllSlotCallWaitingStatus(Context context, SubscriptionManager sm, TelephonyManager tm)83 public static boolean[] getAllSlotCallWaitingStatus(Context context, SubscriptionManager sm, 84 TelephonyManager tm) { 85 int phoneCount = tm.getActiveModemCount(); 86 boolean[] allStatus = new boolean[phoneCount]; 87 88 for (int i = 0; i < phoneCount; i++) { 89 int subId = sm.getSubscriptionIds(i)[0]; 90 boolean callWaitingStatus = getBackupCallWaitingStatus(context, subId); 91 allStatus[i] = callWaitingStatus; 92 } 93 return allStatus; 94 } 95 getAllSlotCallForwardingStatus( Context context, SubscriptionManager sm, TelephonyManager tm)96 public static CallForwardingInfo[] getAllSlotCallForwardingStatus( 97 Context context, SubscriptionManager sm, TelephonyManager tm) { 98 int phoneCount = tm.getActiveModemCount(); 99 CallForwardingInfo[] allStatus = new CallForwardingInfo[phoneCount]; 100 101 for (int i = 0; i < phoneCount; i++) { 102 int subId = sm.getSubscriptionIds(i)[0]; 103 CallForwardingInfo callWaitingStatus = getBackupCallForwardingStatus(context, subId); 104 allStatus[i] = callWaitingStatus; 105 } 106 return allStatus; 107 } 108 clearAllBackupData(Context context, SubscriptionManager sm, TelephonyManager tm)109 public static void clearAllBackupData(Context context, SubscriptionManager sm, 110 TelephonyManager tm) { 111 int phoneCount = tm.getActiveModemCount(); 112 for (int i = 0; i < phoneCount; i++) { 113 int subId = sm.getSubscriptionIds(i)[0]; 114 clearBackupData(context, subId); 115 } 116 } 117 backupPrevStatus(Context context, EnableSmartForwardingTask.SlotUTData[] slotUTData)118 public static void backupPrevStatus(Context context, 119 EnableSmartForwardingTask.SlotUTData[] slotUTData) { 120 for (int i = 0; i < slotUTData.length; i++) { 121 int callWaiting = slotUTData[i].mQueryCallWaiting.result; 122 saveCallWaitingStatus( 123 context, 124 slotUTData[i].subId, 125 callWaiting == TelephonyManager.CALL_WAITING_STATUS_ENABLED); 126 127 saveCallForwardingStatus( 128 context, 129 slotUTData[i].subId, 130 slotUTData[i].mQueryCallForwarding.result); 131 } 132 } 133 getPhoneNumber(Context context, int slotId)134 public static String getPhoneNumber(Context context, int slotId) { 135 SubscriptionManager subscriptionManager = context.getSystemService( 136 SubscriptionManager.class); 137 int[] subIdList = subscriptionManager.getSubscriptionIds(slotId); 138 if (subIdList != null) { 139 SubscriptionInfo subInfo = subscriptionManager.getActiveSubscriptionInfo(subIdList[0]); 140 return (subInfo != null) ? subInfo.getNumber() : ""; 141 } else { 142 return ""; 143 } 144 } 145 }