1 /* 2 * Copyright (C) 2016 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 android.net.cts; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.provider.Settings; 23 import android.test.AndroidTestCase; 24 import android.util.Log; 25 26 import java.lang.Thread; 27 28 public class AirplaneModeTest extends AndroidTestCase { 29 private static final String TAG = "AirplaneModeTest"; 30 private static final String FEATURE_BLUETOOTH = "android.hardware.bluetooth"; 31 private static final String FEATURE_WIFI = "android.hardware.wifi"; 32 private static final int TIMEOUT_MS = 10 * 1000; 33 private boolean mHasFeature; 34 private Context mContext; 35 private ContentResolver resolver; 36 37 public void setup() { 38 mContext= getContext(); 39 resolver = mContext.getContentResolver(); 40 mHasFeature = (mContext.getPackageManager().hasSystemFeature(FEATURE_BLUETOOTH) 41 || mContext.getPackageManager().hasSystemFeature(FEATURE_WIFI)); 42 } 43 44 public void testAirplaneMode() { 45 setup(); 46 if (!mHasFeature) { 47 Log.i(TAG, "The device doesn't support network bluetooth or wifi feature"); 48 return; 49 } 50 51 for (int testCount = 0; testCount < 2; testCount++) { 52 if (!doOneTest()) { 53 fail("Airplane mode failed to change in " + TIMEOUT_MS + "msec"); 54 return; 55 } 56 } 57 } 58 59 private boolean doOneTest() { 60 boolean airplaneModeOn = isAirplaneModeOn(); 61 setAirplaneModeOn(!airplaneModeOn); 62 63 try { 64 Thread.sleep(TIMEOUT_MS); 65 } catch (InterruptedException e) { 66 Log.e(TAG, "Sleep time interrupted.", e); 67 } 68 69 if (airplaneModeOn == isAirplaneModeOn()) { 70 return false; 71 } 72 return true; 73 } 74 75 private void setAirplaneModeOn(boolean enabling) { 76 // Change the system setting for airplane mode 77 Settings.Global.putInt(resolver, Settings.Global.AIRPLANE_MODE_ON, enabling ? 1 : 0); 78 } 79 80 private boolean isAirplaneModeOn() { 81 // Read the system setting for airplane mode 82 return Settings.Global.getInt(mContext.getContentResolver(), 83 Settings.Global.AIRPLANE_MODE_ON, 0) != 0; 84 } 85 } 86