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