1 /*
2  * Copyright (C) 2013 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.internal.telephony.dataconnection;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.Handler;
24 import android.telephony.DataFailCause;
25 
26 import com.android.internal.telephony.Phone;
27 import com.android.internal.telephony.util.TelephonyUtils;
28 import com.android.telephony.Rlog;
29 
30 /**
31  * A package level call that causes all DataConnection bringUp calls to fail a specific
32  * number of times. Here is an example that sets counter to 2 and cause to -3 for all instances:
33  *    adb shell am broadcast -a com.android.internal.telephony.dataconnection.action_fail_bringup \
34  *     --ei counter 2 --ei fail_cause -3
35  *
36  * Also you can add a suggested retry time if desired:
37  *     --ei suggested_retry_time 5000
38  *
39  * The fail_cause is one of {@link DataFailCause}
40  */
41 public class DcTesterFailBringUpAll {
42     private static final String LOG_TAG = "DcTesterFailBrinupAll";
43     private static final boolean DBG = true;
44 
45     private Phone mPhone;
46 
47     private String mActionFailBringUp = DcFailBringUp.INTENT_BASE + "."
48             + DcFailBringUp.ACTION_FAIL_BRINGUP;
49 
50     // The saved FailBringUp data from the intent
51     private DcFailBringUp mFailBringUp = new DcFailBringUp();
52 
53     // The static intent receiver one for all instances.
54     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
55             @Override
56         public void onReceive(Context context, Intent intent) {
57             String action = intent.getAction();
58             if (DBG) log("sIntentReceiver.onReceive: action=" + action);
59             if (action.equals(mActionFailBringUp)) {
60                 mFailBringUp.saveParameters(intent, "sFailBringUp");
61             } else if (action.equals(mPhone.getActionDetached())) {
62                 // Counter is MAX, bringUp/retry will always fail
63                 log("simulate detaching");
64                 mFailBringUp.saveParameters(Integer.MAX_VALUE,
65                         DataFailCause.LOST_CONNECTION,
66                         DcFailBringUp.DEFAULT_SUGGESTED_RETRY_TIME);
67             } else if (action.equals(mPhone.getActionAttached())) {
68                 // Counter is 0 next bringUp/retry will succeed
69                 log("simulate attaching");
70                 mFailBringUp.saveParameters(0, DataFailCause.NONE,
71                         DcFailBringUp.DEFAULT_SUGGESTED_RETRY_TIME);
72             } else {
73                 if (DBG) log("onReceive: unknown action=" + action);
74             }
75         }
76     };
77 
DcTesterFailBringUpAll(Phone phone, Handler handler)78     DcTesterFailBringUpAll(Phone phone, Handler handler) {
79         mPhone = phone;
80         if (TelephonyUtils.IS_DEBUGGABLE) {
81             IntentFilter filter = new IntentFilter();
82 
83             filter.addAction(mActionFailBringUp);
84             log("register for intent action=" + mActionFailBringUp);
85 
86             filter.addAction(mPhone.getActionDetached());
87             log("register for intent action=" + mPhone.getActionDetached());
88 
89             filter.addAction(mPhone.getActionAttached());
90             log("register for intent action=" + mPhone.getActionAttached());
91 
92             phone.getContext().registerReceiver(mIntentReceiver, filter, null, handler);
93         }
94     }
95 
dispose()96     void dispose() {
97         if (TelephonyUtils.IS_DEBUGGABLE) {
98             mPhone.getContext().unregisterReceiver(mIntentReceiver);
99         }
100     }
101 
getDcFailBringUp()102     public DcFailBringUp getDcFailBringUp() {
103         return mFailBringUp;
104     }
105 
log(String s)106     private void log(String s) {
107         Rlog.d(LOG_TAG, s);
108     }
109 }
110