1 /* 2 * Copyright (C) 2008 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 18 package com.android.internal.telephony.cdma; 19 20 import android.content.Context; 21 import android.os.Binder; 22 import android.os.Message; 23 import android.telephony.Rlog; 24 25 import com.android.internal.telephony.IccSmsInterfaceManager; 26 import com.android.internal.telephony.IntRangeManager; 27 import com.android.internal.telephony.SMSDispatcher; 28 import com.android.internal.telephony.uicc.IccUtils; 29 30 import java.util.ArrayList; 31 32 /** 33 * RuimSmsInterfaceManager to provide an inter-process communication to 34 * access Sms in Ruim. 35 */ 36 public class RuimSmsInterfaceManager extends IccSmsInterfaceManager { 37 static final String LOG_TAG = "RuimSmsIM"; 38 static final boolean DBG = true; 39 40 private CdmaBroadcastRangeManager mCdmaBroadcastRangeManager = 41 new CdmaBroadcastRangeManager(); 42 43 public RuimSmsInterfaceManager(CDMAPhone phone, SMSDispatcher dispatcher) { 44 super(phone); 45 mDispatcher = dispatcher; 46 } 47 48 public void dispose() { 49 } 50 51 @Override 52 protected void finalize() { 53 try { 54 super.finalize(); 55 } catch (Throwable throwable) { 56 Rlog.e(LOG_TAG, "Error while finalizing:", throwable); 57 } 58 if(DBG) Rlog.d(LOG_TAG, "RuimSmsInterfaceManager finalized"); 59 } 60 61 @Override 62 protected void deleteSms(int index, Message response) { 63 mPhone.mCi.deleteSmsOnRuim(index, response); 64 } 65 66 @Override 67 protected void writeSms(int status, byte[] pdu, byte[] smsc, Message response) { 68 //NOTE smsc not used in RUIM 69 mPhone.mCi.writeSmsToRuim(status, IccUtils.bytesToHexString(pdu), 70 response); 71 } 72 73 @Override 74 public boolean enableCellBroadcast(int messageIdentifier) { 75 return enableCellBroadcastRange(messageIdentifier, messageIdentifier); 76 } 77 78 @Override 79 public boolean disableCellBroadcast(int messageIdentifier) { 80 return disableCellBroadcastRange(messageIdentifier, messageIdentifier); 81 } 82 83 @Override 84 public boolean enableCellBroadcastRange(int startMessageId, int endMessageId) { 85 if (DBG) log("enableCellBroadcastRange"); 86 87 Context context = mPhone.getContext(); 88 89 context.enforceCallingPermission( 90 "android.permission.RECEIVE_SMS", 91 "Enabling cdma broadcast SMS"); 92 93 String client = context.getPackageManager().getNameForUid( 94 Binder.getCallingUid()); 95 96 if (!mCdmaBroadcastRangeManager.enableRange(startMessageId, endMessageId, client)) { 97 log("Failed to add cdma broadcast subscription for MID range " + startMessageId 98 + " to " + endMessageId + " from client " + client); 99 return false; 100 } 101 102 if (DBG) 103 log("Added cdma broadcast subscription for MID range " + startMessageId 104 + " to " + endMessageId + " from client " + client); 105 106 setCdmaBroadcastActivation(!mCdmaBroadcastRangeManager.isEmpty()); 107 108 return true; 109 } 110 111 @Override 112 public boolean disableCellBroadcastRange(int startMessageId, int endMessageId) { 113 if (DBG) log("disableCellBroadcastRange"); 114 115 Context context = mPhone.getContext(); 116 117 context.enforceCallingPermission( 118 "android.permission.RECEIVE_SMS", 119 "Disabling cell broadcast SMS"); 120 121 String client = context.getPackageManager().getNameForUid( 122 Binder.getCallingUid()); 123 124 if (!mCdmaBroadcastRangeManager.disableRange(startMessageId, endMessageId, client)) { 125 log("Failed to remove cdma broadcast subscription for MID range " + startMessageId 126 + " to " + endMessageId + " from client " + client); 127 return false; 128 } 129 130 if (DBG) 131 log("Removed cdma broadcast subscription for MID range " + startMessageId 132 + " to " + endMessageId + " from client " + client); 133 134 setCdmaBroadcastActivation(!mCdmaBroadcastRangeManager.isEmpty()); 135 136 return true; 137 } 138 139 class CdmaBroadcastRangeManager extends IntRangeManager { 140 private ArrayList<CdmaSmsBroadcastConfigInfo> mConfigList = 141 new ArrayList<CdmaSmsBroadcastConfigInfo>(); 142 143 /** 144 * Called when the list of enabled ranges has changed. This will be 145 * followed by zero or more calls to {@link #addRange} followed by 146 * a call to {@link #finishUpdate}. 147 */ 148 @Override 149 protected void startUpdate() { 150 mConfigList.clear(); 151 } 152 153 /** 154 * Called after {@link #startUpdate} to indicate a range of enabled 155 * values. 156 * @param startId the first id included in the range 157 * @param endId the last id included in the range 158 */ 159 @Override 160 protected void addRange(int startId, int endId, boolean selected) { 161 mConfigList.add(new CdmaSmsBroadcastConfigInfo(startId, endId, 162 1, selected)); 163 } 164 165 /** 166 * Called to indicate the end of a range update started by the 167 * previous call to {@link #startUpdate}. 168 * @return true if successful, false otherwise 169 */ 170 @Override 171 protected boolean finishUpdate() { 172 if (mConfigList.isEmpty()) { 173 return true; 174 } else { 175 CdmaSmsBroadcastConfigInfo[] configs = 176 mConfigList.toArray(new CdmaSmsBroadcastConfigInfo[mConfigList.size()]); 177 return setCdmaBroadcastConfig(configs); 178 } 179 } 180 } 181 182 private boolean setCdmaBroadcastConfig(CdmaSmsBroadcastConfigInfo[] configs) { 183 if (DBG) 184 log("Calling setCdmaBroadcastConfig with " + configs.length + " configurations"); 185 186 synchronized (mLock) { 187 Message response = mHandler.obtainMessage(EVENT_SET_BROADCAST_CONFIG_DONE); 188 189 mSuccess = false; 190 mPhone.mCi.setCdmaBroadcastConfig(configs, response); 191 192 try { 193 mLock.wait(); 194 } catch (InterruptedException e) { 195 log("interrupted while trying to set cdma broadcast config"); 196 } 197 } 198 199 return mSuccess; 200 } 201 202 private boolean setCdmaBroadcastActivation(boolean activate) { 203 if (DBG) 204 log("Calling setCdmaBroadcastActivation(" + activate + ")"); 205 206 synchronized (mLock) { 207 Message response = mHandler.obtainMessage(EVENT_SET_BROADCAST_ACTIVATION_DONE); 208 209 mSuccess = false; 210 mPhone.mCi.setCdmaBroadcastActivation(activate, response); 211 212 try { 213 mLock.wait(); 214 } catch (InterruptedException e) { 215 log("interrupted while trying to set cdma broadcast activation"); 216 } 217 } 218 219 return mSuccess; 220 } 221 222 @Override 223 protected void log(String msg) { 224 Rlog.d(LOG_TAG, "[RuimSmsInterfaceManager] " + msg); 225 } 226 } 227 228