• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.settings.development;
18 
19 import android.content.Context;
20 import android.debug.IAdbManager;
21 import android.net.ConnectivityManager;
22 import android.net.LinkAddress;
23 import android.net.LinkProperties;
24 import android.net.wifi.WifiManager;
25 import android.os.RemoteException;
26 import android.os.ServiceManager;
27 import android.util.Log;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 import com.android.settingslib.deviceinfo.AbstractConnectivityPreferenceController;
35 
36 import java.net.Inet4Address;
37 import java.net.InetAddress;
38 import java.util.Iterator;
39 
40 /**
41  * Controller for the ip address preference in the Wireless debugging
42  * fragment.
43  */
44 public class AdbIpAddressPreferenceController extends AbstractConnectivityPreferenceController {
45     private static final String TAG = "AdbIpAddrPrefCtrl";
46 
47     private static final String[] CONNECTIVITY_INTENTS = {
48             ConnectivityManager.CONNECTIVITY_ACTION,
49             WifiManager.ACTION_LINK_CONFIGURATION_CHANGED,
50             WifiManager.NETWORK_STATE_CHANGED_ACTION,
51     };
52 
53     private static final String PREF_KEY = "adb_ip_addr_pref";
54     private Preference mAdbIpAddrPref;
55     private int mPort;
56     private final ConnectivityManager mCM;
57     private IAdbManager mAdbManager;
58 
AdbIpAddressPreferenceController(Context context, Lifecycle lifecycle)59     public AdbIpAddressPreferenceController(Context context, Lifecycle lifecycle) {
60         super(context, lifecycle);
61         mCM = context.getSystemService(ConnectivityManager.class);
62         mAdbManager = IAdbManager.Stub.asInterface(ServiceManager.getService(
63                 Context.ADB_SERVICE));
64     }
65 
66     @Override
isAvailable()67     public boolean isAvailable() {
68         return true;
69     }
70 
71     @Override
getPreferenceKey()72     public String getPreferenceKey() {
73         return PREF_KEY;
74     }
75 
76     @Override
displayPreference(PreferenceScreen screen)77     public void displayPreference(PreferenceScreen screen) {
78         super.displayPreference(screen);
79         mAdbIpAddrPref = screen.findPreference(PREF_KEY);
80         updateConnectivity();
81     }
82 
83     @Override
updateState(Preference preference)84     public void updateState(Preference preference) {
85         super.updateState(preference);
86         updateConnectivity();
87     }
88 
89     @Override
getConnectivityIntents()90     protected String[] getConnectivityIntents() {
91         return CONNECTIVITY_INTENTS;
92     }
93 
getPort()94     protected int getPort() {
95         try {
96             return mAdbManager.getAdbWirelessPort();
97         } catch (RemoteException e) {
98             Log.e(TAG, "Unable to get the adbwifi port");
99         }
100         return 0;
101     }
102 
getIpv4Address()103     public String getIpv4Address() {
104         return getDefaultIpAddresses(mCM);
105     }
106 
107     @Override
updateConnectivity()108     protected void updateConnectivity() {
109         String ipAddress = getDefaultIpAddresses(mCM);
110         if (ipAddress != null) {
111             int port = getPort();
112             if (port <= 0) {
113                 mAdbIpAddrPref.setSummary(R.string.status_unavailable);
114             } else {
115                 ipAddress += ":" + port;
116             }
117             mAdbIpAddrPref.setSummary(ipAddress);
118         } else {
119             mAdbIpAddrPref.setSummary(R.string.status_unavailable);
120         }
121     }
122 
123     /**
124      * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style
125      * addresses.
126      * @param cm ConnectivityManager
127      * @return the formatted and newline-separated IP addresses, or null if none.
128      */
getDefaultIpAddresses(ConnectivityManager cm)129     private static String getDefaultIpAddresses(ConnectivityManager cm) {
130         LinkProperties prop = cm.getLinkProperties(cm.getActiveNetwork());
131         return formatIpAddresses(prop);
132     }
133 
formatIpAddresses(LinkProperties prop)134     private static String formatIpAddresses(LinkProperties prop) {
135         if (prop == null) {
136             return null;
137         }
138 
139         Iterator<LinkAddress> iter = prop.getAllLinkAddresses().iterator();
140         // If there are no entries, return null
141         if (!iter.hasNext()) {
142             return null;
143         }
144 
145         // Concatenate all available addresses, newline separated
146         StringBuilder addresses = new StringBuilder();
147         while (iter.hasNext()) {
148             InetAddress addr = iter.next().getAddress();
149             if (addr instanceof Inet4Address) {
150                 // adb only supports ipv4 at the moment
151                 addresses.append(addr.getHostAddress());
152                 break;
153             }
154         }
155         return addresses.toString();
156     }
157 }
158