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 package com.android.sdksetup;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.hardware.input.InputManager;
24 import android.hardware.input.KeyboardLayout;
25 import android.location.LocationManager;
26 import android.net.wifi.WifiManager;
27 import android.net.wifi.WifiConfiguration;
28 import android.provider.Settings;
29 import android.os.Bundle;
30 import android.os.RemoteException;
31 import android.os.ServiceManager;
32 import android.os.SystemProperties;
33 import android.os.Build;
34 import android.telephony.TelephonyManager;
35 import android.util.Log;
36 import android.view.InputDevice;
37 
38 /**
39  * Entry point for SDK SetupWizard.
40  *
41  */
42 public class DefaultActivity extends Activity {
43     private static final String TAG = "SdkSetup";
44     private static final int ADD_NETWORK_FAIL = -1;
45     @Override
onCreate(Bundle icicle)46     protected void onCreate(Bundle icicle) {
47         super.onCreate(icicle);
48 
49         // Edit Settings only for Emulator
50         if (Build.IS_EMULATOR) {
51             // Add network with SSID "AndroidWifi"
52             WifiConfiguration config = new WifiConfiguration();
53             config.SSID = "\"AndroidWifi\"";
54             config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN);
55             WifiManager mWifiManager = getApplicationContext().getSystemService(WifiManager.class);
56             int netId = mWifiManager.addNetwork(config);
57             if (netId == ADD_NETWORK_FAIL || mWifiManager.enableNetwork(netId, true)) {
58                 Log.e(TAG, "Unable to add Wi-Fi network AndroidWifi.");
59             }
60 
61             // Set physical keyboard layout based on the system property set by emulator host.
62             String layoutName = SystemProperties.get("vendor.qemu.keyboard_layout");
63             String displaySettingsName = SystemProperties.get("ro.boot.qemu.display.settings.xml");
64             String deviceName = "qwerty2";
65             InputDevice device = getKeyboardDevice(deviceName);
66             if (device != null && !layoutName.isEmpty()) {
67                 setKeyboardLayout(device, layoutName);
68             }
69             // Add a persistent setting to allow other apps to know the device has been provisioned.
70             Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);
71 
72             Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1);
73 
74             // Disables a dialog shown on adb install execution.
75             Settings.Global.putInt(getContentResolver(), Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, 0);
76 
77             // Enable the GPS.
78             // Not needed since this SDK will contain the Settings app.
79             Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
80                     LocationManager.GPS_PROVIDER);
81 
82             // enable install from non market
83             Settings.Secure.putInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 1);
84 
85             Settings.Global.putInt(getContentResolver(), Settings.Global.ADB_ENABLED, 1);
86 
87             // Disable offload wifi tethering
88             Settings.Global.putInt(getContentResolver(), Settings.Global.TETHER_OFFLOAD_DISABLED, 1);
89 
90             // b/193418404
91             // the following blocks, TODO: find out why and fix it. disable this for now.
92             // TelephonyManager mTelephony = getApplicationContext().getSystemService(TelephonyManager.class);
93             // mTelephony.setPreferredNetworkTypeBitmask(TelephonyManager.NETWORK_TYPE_BITMASK_NR);
94             if ("freeform".equals(displaySettingsName)) {
95                 Settings.Global.putInt(getContentResolver(), "sf", 1);
96                 Settings.Global.putString(getContentResolver(), Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, "1");
97                 Settings.Global.putString(getContentResolver(), Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, "1");
98                 Settings.Global.putString(getContentResolver(), Settings.Global.DEVELOPMENT_WM_DISPLAY_SETTINGS_PATH, "vendor/etc/display_settings_freeform.xml");
99             } else if ("resizable".equals(displaySettingsName)) {
100             // Enable auto rotate for resizable AVD
101             Settings.System.putString(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, "1");
102             }
103         }
104 
105         // remove this activity from the package manager.
106         PackageManager pm = getPackageManager();
107         ComponentName name = new ComponentName(this, DefaultActivity.class);
108         pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
109 
110         // terminate the activity.
111         finish();
112     }
113 
getKeyboardDevice(String keyboardDeviceName)114     private InputDevice getKeyboardDevice(String keyboardDeviceName) {
115         int[] deviceIds = InputDevice.getDeviceIds();
116 
117         for (int deviceId : deviceIds) {
118             InputDevice inputDevice = InputDevice.getDevice(deviceId);
119             if (inputDevice != null
120                     && inputDevice.supportsSource(InputDevice.SOURCE_KEYBOARD)
121                     && inputDevice.getName().equals(keyboardDeviceName)) {
122                 return inputDevice;
123             }
124         }
125         return null;
126     }
127 
setKeyboardLayout(InputDevice keyboardDevice, String layoutName)128     private void setKeyboardLayout(InputDevice keyboardDevice, String layoutName) {
129         InputManager im = InputManager.getInstance();
130 
131         KeyboardLayout[] keyboardLayouts =
132                 im.getKeyboardLayoutsForInputDevice(keyboardDevice.getIdentifier());
133 
134         for (KeyboardLayout keyboardLayout : keyboardLayouts) {
135             if (keyboardLayout.getDescriptor().endsWith(layoutName)) {
136                 im.setCurrentKeyboardLayoutForInputDevice(
137                         keyboardDevice.getIdentifier(), keyboardLayout.getDescriptor());
138                 return;
139             }
140         }
141     }
142 }
143 
144