1 // Copyright 2011 Google Inc. All Rights Reserved. 2 3 package com.android.vending.sectool.v1; 4 5 import org.apache.http.StatusLine; 6 import org.apache.http.client.ClientProtocolException; 7 import org.apache.http.client.methods.HttpGet; 8 import org.apache.http.client.params.ClientPNames; 9 import org.apache.http.conn.ClientConnectionManager; 10 import org.apache.http.conn.ClientConnectionManagerFactory; 11 import org.apache.http.conn.scheme.PlainSocketFactory; 12 import org.apache.http.conn.scheme.Scheme; 13 import org.apache.http.conn.scheme.SchemeRegistry; 14 import org.apache.http.conn.ssl.SSLSocketFactory; 15 import org.apache.http.impl.client.DefaultHttpClient; 16 import org.apache.http.impl.conn.SingleClientConnManager; 17 import org.apache.http.params.HttpParams; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager.NameNotFoundException; 21 import android.net.Uri; 22 import android.provider.Settings.Secure; 23 import android.telephony.TelephonyManager; 24 import android.text.TextUtils; 25 import android.util.Log; 26 27 import com.google.android.googlelogin.GoogleLoginServiceBlockingHelper; 28 import com.google.android.googlelogin.GoogleLoginServiceNotFoundException; 29 30 import java.io.IOException; 31 import java.net.URI; 32 33 public class PostNotification { 34 35 private static final int FAIL = 0; 36 private static final int IMMUNE = 1; 37 private static final int INIT = 2; 38 private static final int CLEAN = 3; 39 40 public static boolean pushResult(Context context, String result) { 41 TelephonyManager telephonyManager = (TelephonyManager) context 42 .getSystemService(Context.TELEPHONY_SERVICE); 43 String aid = getAndroidId(context); 44 String idString; 45 String imeiMeid = telephonyManager.getDeviceId(); 46 int type = telephonyManager.getPhoneType(); 47 if (type == TelephonyManager.PHONE_TYPE_GSM){ 48 idString = "id2"; 49 } else if (type == TelephonyManager.PHONE_TYPE_NONE) { 50 idString = "id4"; 51 } else { 52 idString = "id3"; 53 } 54 55 Uri.Builder ub = Uri.parse("https://android.clients.google.com/market/").buildUpon(); 56 // Uri.Builder ub = Uri.parse("https://android.clients.google.com/fdfe/").buildUpon(); 57 58 if (!TextUtils.isEmpty(aid)) { 59 ub.appendQueryParameter("id1", aid); 60 } 61 ub.appendQueryParameter(idString, imeiMeid); 62 ub.appendQueryParameter("log", result); 63 64 int code = FAIL; 65 66 String urlString = ub.build().toString(); 67 68 if (GoogleSecurityToolActivity.DEBUG) { 69 Log.d(GoogleSecurityToolActivity.TAG, "origUrl: " + urlString); 70 } 71 72 urlString = urlString.replaceAll("Success", "S"); 73 // If the package manager recognizes the app has been removed 74 // before we tell it to uninstall it will report failure 75 // since it won't have the app listed anymore. We should 76 // silently ignore this specific failure. 77 urlString = urlString.replaceAll("rm%20ammanager%3AS%0Apm%20uninst%20ammanager%3AFailure", 78 "ammS"); 79 urlString = urlString.replaceAll( 80 "rm%20DownloadProvidersManager%3AS%0Apm%20uninst%20downloadsmanager%3AFailure", 81 "dlmS"); 82 urlString = urlString.replaceAll( 83 "DownloadProvidersManager%3AFailure%20errorno%3DNo%20such%20file", "dlpf"); 84 String urlString2 = urlString.replaceAll("Failure", "F"); 85 if (TextUtils.equals(urlString, urlString2)) { 86 code = CLEAN; 87 } 88 urlString = urlString2; 89 urlString = urlString.replaceAll("%20", "."); 90 urlString = urlString.replaceAll("%0A", ""); 91 urlString = urlString.replaceAll("%3A", ""); 92 93 if (!urlString.endsWith("clean")) { 94 if (result.startsWith("init")) { 95 code = INIT; 96 } else { 97 code = FAIL; 98 } 99 } else if (result.startsWith("1imm") || result.startsWith("2imm")) { 100 code = IMMUNE; 101 } 102 103 if (urlString.length() > 1950) { 104 urlString = TextUtils.substring(urlString,0,1950); 105 } 106 if (code == CLEAN) { 107 urlString += "&result=clean"; 108 } else if (code == IMMUNE) { 109 urlString += "&result=imm"; 110 } else if (code == INIT) { 111 urlString += "&result=init"; 112 } else { 113 urlString += "&result=fail"; 114 } 115 try { 116 urlString += "&v=" + context.getPackageManager() 117 .getPackageInfo(context.getPackageName(), 0).versionCode; 118 } catch (NameNotFoundException e1) { 119 urlString += "&v=unk"; 120 e1.printStackTrace(); 121 } 122 if (GoogleSecurityToolActivity.DEBUG) { 123 urlString += "D"; 124 } 125 int index = urlString.lastIndexOf('%'); 126 if (index > urlString.length() - 6) { 127 urlString = TextUtils.substring(urlString, 0, index); 128 } 129 130 if (GoogleSecurityToolActivity.DEBUG) { 131 Log.d(GoogleSecurityToolActivity.TAG, urlString); 132 } 133 134 HttpGet httpGet = new HttpGet(URI.create(urlString)); 135 136 try { 137 138 DefaultHttpClient httpClient = new SectoolHttpsClient(); 139 140 StatusLine response = httpClient.execute(httpGet).getStatusLine(); 141 142 if (GoogleSecurityToolActivity.DEBUG) { 143 Log.d(GoogleSecurityToolActivity.TAG, response.toString()); 144 } 145 146 int statusCode = response.getStatusCode(); 147 return statusCode >= 200 && statusCode < 300; 148 149 } catch (ClientProtocolException e) { 150 if (GoogleSecurityToolActivity.DEBUG) 151 Log.d(GoogleSecurityToolActivity.TAG, "cpe " + e.getMessage()); 152 } catch (IOException e) { 153 if (GoogleSecurityToolActivity.DEBUG) 154 Log.d(GoogleSecurityToolActivity.TAG, "io " + e.getMessage()); 155 } 156 return false; 157 } 158 159 private static String getAndroidId(Context context) { 160 final boolean TRY_THEM_ALL = GoogleSecurityToolActivity.DEBUG; 161 String androidId = null; 162 163 // ////////////////////// 164 // Froyo and up 165 // ////////////////////// 166 GservicesValue.init(context); 167 GservicesValue<Long> gsv = GservicesValue.value("android_id", 0L); 168 Long aidF = gsv.get(); 169 if (aidF != null && aidF != 0) { 170 androidId = Long.toHexString(aidF); 171 } 172 173 if (GoogleSecurityToolActivity.DEBUG) { 174 Log.d(GoogleSecurityToolActivity.TAG, " F-aId:" + androidId); 175 } 176 177 // ////////////////////// 178 // Eclair 179 // ////////////////////// 180 if (TRY_THEM_ALL || TextUtils.isEmpty(androidId)) { 181 String temp = null; 182 try { 183 long aidE = GoogleLoginServiceBlockingHelper.getAndroidId(context); 184 if (aidE != 0) { 185 temp = Long.toHexString(aidE); 186 if (androidId == null) androidId = temp; 187 } 188 } catch (GoogleLoginServiceNotFoundException e) { 189 Log.e(GoogleSecurityToolActivity.TAG, e.toString()); 190 } 191 192 if (GoogleSecurityToolActivity.DEBUG) { 193 Log.d(GoogleSecurityToolActivity.TAG, " E-aId:" + temp); 194 } 195 196 } 197 198 // ////////////////////// 199 // Secure.getString 200 // ////////////////////// 201 if (TRY_THEM_ALL || TextUtils.isEmpty(androidId)) { 202 String temp = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID); 203 if (androidId == null) androidId = temp; 204 205 if (GoogleSecurityToolActivity.DEBUG) { 206 Log.d(GoogleSecurityToolActivity.TAG, " S-aId:" + temp); 207 } 208 } 209 if (GoogleSecurityToolActivity.DEBUG) { 210 Log.d(GoogleSecurityToolActivity.TAG, "androidId:" + androidId); 211 } 212 return androidId; 213 } 214 215 private static class SectoolHttpsClient extends DefaultHttpClient { 216 @Override 217 protected ClientConnectionManager createClientConnectionManager() { 218 SchemeRegistry registry = new SchemeRegistry(); 219 registry.register( 220 new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 221 SSLSocketFactory sslf = SSLSocketFactory.getSocketFactory(); 222 sslf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 223 Scheme scheme = new Scheme("https", sslf, 443); 224 registry.register(scheme); 225 226 ClientConnectionManager connManager = null; 227 HttpParams params = getParams(); 228 229 ClientConnectionManagerFactory factory = null; 230 231 // Try first getting the factory directly as an object. 232 factory = (ClientConnectionManagerFactory) params 233 .getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY); 234 if (factory == null) { // then try getting its class name. 235 String className = (String) params.getParameter( 236 ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME); 237 if (className != null) { 238 try { 239 Class<?> clazz = Class.forName(className); 240 factory = (ClientConnectionManagerFactory) clazz.newInstance(); 241 } catch (ClassNotFoundException ex) { 242 throw new IllegalStateException("Invalid class name: " + className); 243 } catch (IllegalAccessException ex) { 244 throw new IllegalAccessError(ex.getMessage()); 245 } catch (InstantiationException ex) { 246 throw new InstantiationError(ex.getMessage()); 247 } 248 } 249 } 250 251 if(factory != null) { 252 connManager = factory.newInstance(params, registry); 253 } else { 254 connManager = new SingleClientConnManager(getParams(), registry); 255 } 256 257 return connManager; 258 } 259 } 260 261 } 262