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, TelephonyManager tm)83 public static boolean[] getAllSlotCallWaitingStatus(Context context, TelephonyManager tm) { 84 int phoneCount = tm.getActiveModemCount(); 85 boolean[] allStatus = new boolean[phoneCount]; 86 87 for (int i = 0; i < phoneCount; i++) { 88 int subId = SubscriptionManager.getSubscriptionId(i); 89 boolean callWaitingStatus = getBackupCallWaitingStatus(context, subId); 90 allStatus[i] = callWaitingStatus; 91 } 92 return allStatus; 93 } 94 getAllSlotCallForwardingStatus( Context context, SubscriptionManager sm, TelephonyManager tm)95 public static CallForwardingInfo[] getAllSlotCallForwardingStatus( 96 Context context, SubscriptionManager sm, TelephonyManager tm) { 97 int phoneCount = tm.getActiveModemCount(); 98 CallForwardingInfo[] allStatus = new CallForwardingInfo[phoneCount]; 99 100 for (int i = 0; i < phoneCount; i++) { 101 int subId = SubscriptionManager.getSubscriptionId(i); 102 CallForwardingInfo callWaitingStatus = getBackupCallForwardingStatus(context, subId); 103 allStatus[i] = callWaitingStatus; 104 } 105 return allStatus; 106 } 107 clearAllBackupData(Context context, SubscriptionManager sm, TelephonyManager tm)108 public static void clearAllBackupData(Context context, SubscriptionManager sm, 109 TelephonyManager tm) { 110 int phoneCount = tm.getActiveModemCount(); 111 for (int i = 0; i < phoneCount; i++) { 112 int subId = SubscriptionManager.getSubscriptionId(i); 113 clearBackupData(context, subId); 114 } 115 } 116 backupPrevStatus(Context context, EnableSmartForwardingTask.SlotUTData[] slotUTData)117 public static void backupPrevStatus(Context context, 118 EnableSmartForwardingTask.SlotUTData[] slotUTData) { 119 for (int i = 0; i < slotUTData.length; i++) { 120 int callWaiting = slotUTData[i].mQueryCallWaiting.result; 121 saveCallWaitingStatus( 122 context, 123 slotUTData[i].subId, 124 callWaiting == TelephonyManager.CALL_WAITING_STATUS_ENABLED); 125 126 saveCallForwardingStatus( 127 context, 128 slotUTData[i].subId, 129 slotUTData[i].mQueryCallForwarding.result); 130 } 131 } 132 getPhoneNumber(Context context, int slotId)133 public static String getPhoneNumber(Context context, int slotId) { 134 SubscriptionManager subscriptionManager = context.getSystemService( 135 SubscriptionManager.class); 136 SubscriptionInfo subInfo = subscriptionManager.getActiveSubscriptionInfo( 137 SubscriptionManager.getSubscriptionId(slotId)); 138 return (subInfo != null) ? subInfo.getNumber() : ""; 139 } 140 }