6 package com.google.appinventor.components.runtime;
8 import android.app.Activity;
10 import android.content.Context;
11 import android.content.Intent;
13 import android.content.pm.PackageManager.NameNotFoundException;
15 import android.net.ConnectivityManager;
16 import android.net.DhcpInfo;
17 import android.net.NetworkInfo;
18 import android.net.Uri;
19 import android.net.wifi.WifiManager;
21 import android.os.Build;
23 import android.util.Log;
46 import java.lang.reflect.InvocationTargetException;
47 import java.lang.reflect.Method;
48 import java.security.MessageDigest;
50 import java.util.Formatter;
61 @DesignerComponent(version = YaVersion.PHONESTATUS_COMPONENT_VERSION,
62 description =
"Component that returns information about the phone.",
63 category = ComponentCategory.INTERNAL,
65 iconName =
"images/phoneip.png")
67 @UsesLibraries(libraries =
"webrtc.jar," +
68 "google-http-client-beta.jar," +
69 "google-http-client-android2-beta.jar," +
70 "google-http-client-android3-beta.jar")
71 @UsesNativeLibraries(v7aLibraries =
"libjingle_peerconnection_so.so",
72 v8aLibraries =
"libjingle_peerconnection_so.so",
73 x86_64Libraries =
"libjingle_peerconnection_so.so")
76 private static Activity activity;
77 private static final String LOG_TAG =
"PhoneStatus";
78 private final Form form;
80 private static boolean useWebRTC =
false;
81 private String firstSeed =
null;
82 private String firstHmacSeed =
null;
85 super(container.
$form());
86 this.form = container.
$form();
88 if (mainInstance ==
null) {
93 @
SimpleFunction(description =
"Returns the IP address of the phone in the form of a String")
94 public static String GetWifiIpAddress() {
96 Object wifiManager = activity.getSystemService(
"wifi");
97 ip = ((WifiManager) wifiManager).getDhcpInfo();
98 int s_ipAddress= ip.ipAddress;
101 ipAddress = intToIp(s_ipAddress);
103 ipAddress =
"Error: No Wifi Connection";
107 @
SimpleFunction(description =
"Returns TRUE if the phone is on Wifi, FALSE otherwise")
108 public static
boolean isConnected() {
109 ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(
"connectivity");
110 NetworkInfo networkInfo =
null;
111 if (connectivityManager !=
null) {
113 connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
115 return networkInfo ==
null ? false : networkInfo.isConnected();
118 @
SimpleFunction(description =
"Establish the secret seed for HOTP generation. " +
119 "Return the SHA1 of the provided seed, this will be used to contact the " +
120 "rendezvous server. Note: This code also starts the connection negotiation " +
121 "process if we are using WebRTC. This is a bit of a kludge...")
122 public String setHmacSeedReturnCode(String seed, String rendezvousServer) {
125 if (seed.equals(
"")) {
144 if (firstSeed !=
null) {
145 if (!firstSeed.equals(seed)) {
149 "You cannot use two codes with one start up of the Companion. You should restart the " +
150 "Companion and try again.",
151 "Warning",
"OK",
new Runnable() {
152 @Override
public void run() {
161 return firstHmacSeed;
180 Sha1 = MessageDigest.getInstance(
"SHA1");
181 }
catch (Exception e) {
182 Log.e(LOG_TAG,
"Exception getting SHA1 Instance", e);
185 Sha1.update(seed.getBytes());
186 byte [] result = Sha1.digest();
187 StringBuffer sb =
new StringBuffer(result.length * 2);
188 Formatter formatter =
new Formatter(sb);
189 for (
byte b : result) {
190 formatter.format(
"%02x", b);
192 Log.d(LOG_TAG,
"Seed = " + seed);
193 Log.d(LOG_TAG,
"Code = " + sb.toString());
194 firstHmacSeed = sb.toString();
195 return firstHmacSeed;
198 @
SimpleFunction(description =
"Returns true if we are running in the emulator or USB Connection")
199 public
boolean isDirect() {
200 Log.d(LOG_TAG,
"android.os.Build.VERSION.RELEASE = " + android.os.Build.VERSION.RELEASE);
201 Log.d(LOG_TAG,
"android.os.Build.PRODUCT = " + android.os.Build.PRODUCT);
213 public
void startWebRTC(String rendezvousServer, String iceServers) {
219 ((
ReplForm)form).setWebRTCMgr(webRTCNativeMgr);
222 @
SimpleFunction(description =
"Start the internal AppInvHTTPD to listen for incoming forms. FOR REPL USE ONLY!")
223 public
void startHTTPD(
boolean secure) {
225 ((
ReplForm) form).startHTTPD(secure);
229 @
SimpleFunction(description =
"Declare that we have loaded our initial assets and other assets should come from the sdcard")
230 public
void setAssetsLoaded() {
232 ((
ReplForm) form).setAssetsLoaded();
236 @
SimpleFunction(description =
"Causes an Exception, used to debug exception processing.")
237 public static
void doFault() throws Exception {
238 throw new Exception(
"doFault called!");
248 @
SimpleFunction(description =
"Downloads the URL and installs it as an Android Package via the installed browser")
249 public
void installURL(String url) {
251 Class<?> clazz = Class.forName(
"edu.mit.appinventor.companionextras.CompanionExtras");
252 Object o = clazz.getConstructor(
Form.class).newInstance(form);
253 Method m = clazz.getMethod(
"Extra1", String.class);
255 }
catch (Exception e) {
257 Uri uri = Uri.parse(url +
"?store=1");
258 Intent intent =
new Intent(Intent.ACTION_VIEW).setData(uri);
259 form.startActivity(intent);
264 public
void shutdown() {
286 public
void WebRTC(
boolean useWebRTC) {
287 this.useWebRTC = useWebRTC;
291 public
boolean WebRTC() {
305 @
SimpleFunction(description =
"Get the current Android SDK Level")
325 @
SimpleFunction(description =
"Return the our VersionName property")
326 public String GetVersionName() {
328 String packageName = form.getPackageName();
329 return form.getPackageManager().getPackageInfo(packageName, 0).versionName;
330 }
catch (NameNotFoundException e) {
331 Log.e(LOG_TAG,
"Unable to get VersionName", e);
337 public String GetInstaller() {
340 if (installer ==
null) {
351 public String InstallationId() {
352 return org.acra.util.Installation.id(form);
365 static void doSettings() {
366 Log.d(LOG_TAG,
"doSettings called.");
367 if (mainInstance !=
null) {
370 Log.d(LOG_TAG,
"mainStance is null on doSettings");
375 return (i & 0xFF) +
"." + ((i >> 8) & 0xFF) +
"." + ((i >> 16) & 0xFF) +
"." + ((i >>24) & 0xFF);