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