PageRenderTime 57ms CodeModel.GetById 13ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/build/mobile/sutagent/android/SUTAgentAndroid.java

http://github.com/zpao/v8monkey
Java | 681 lines | 524 code | 87 blank | 70 comment | 92 complexity | 890c70e514cc024e76aa3836475c4a20 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is Android SUTAgent code.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Bob Moss.
 18 * Portions created by the Initial Developer are Copyright (C) 2010
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *  Bob Moss <bmoss@mozilla.com>
 23 *
 24 * Alternatively, the contents of this file may be used under the terms of
 25 * either the GNU General Public License Version 2 or later (the "GPL"), or
 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 27 * in which case the provisions of the GPL or the LGPL are applicable instead
 28 * of those above. If you wish to allow use of your version of this file only
 29 * under the terms of either the GPL or the LGPL, and not to allow others to
 30 * use your version of this file under the terms of the MPL, indicate your
 31 * decision by deleting the provisions above and replace them with the notice
 32 * and other provisions required by the GPL or the LGPL. If you do not delete
 33 * the provisions above, a recipient may use your version of this file under
 34 * the terms of any one of the MPL, the GPL or the LGPL.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37
 38package com.mozilla.SUTAgentAndroid;
 39
 40import java.io.File;
 41import java.io.PrintWriter;
 42import java.net.InetAddress;
 43import java.net.NetworkInterface;
 44import java.net.SocketException;
 45import java.util.Enumeration;
 46import java.util.List;
 47import java.util.Timer;
 48
 49import com.mozilla.SUTAgentAndroid.service.ASMozStub;
 50import com.mozilla.SUTAgentAndroid.service.DoCommand;
 51import android.app.Activity;
 52import android.bluetooth.BluetoothAdapter;
 53import android.content.BroadcastReceiver;
 54import android.content.Context;
 55import android.content.Intent;
 56import android.content.IntentFilter;
 57import android.content.pm.ActivityInfo;
 58import android.content.res.Configuration;
 59import android.net.Uri;
 60import android.net.wifi.SupplicantState;
 61import android.net.wifi.WifiConfiguration;
 62import android.net.wifi.WifiInfo;
 63import android.net.wifi.WifiManager;
 64import android.net.wifi.WifiManager.WifiLock;
 65import android.os.BatteryManager;
 66import android.os.Bundle;
 67import android.os.Handler;
 68import android.telephony.TelephonyManager;
 69import android.util.Log;
 70import android.view.Menu;
 71import android.view.MenuItem;
 72import android.view.View;
 73import android.view.View.OnClickListener;
 74import android.widget.Button;
 75import android.widget.TextView;
 76import android.widget.Toast;
 77
 78public class SUTAgentAndroid extends Activity
 79    {
 80    final Handler mHandler = new Handler();
 81
 82    public static final int START_PRG = 1959;
 83    MenuItem mExitMenuItem;
 84    Timer timer = null;
 85
 86    public static String sUniqueID = null;
 87    public static String sLocalIPAddr = null;
 88    public static String sACStatus = null;
 89    public static String sPowerStatus = null;
 90    public static int    nChargeLevel = 0;
 91    public static int    nBatteryTemp = 0;
 92
 93    String lineSep = System.getProperty("line.separator");
 94    public PrintWriter dataOut = null;
 95
 96    private static boolean bNetworkingStarted = false;
 97    private static String RegSvrIPAddr = "";
 98    private static String RegSvrIPPort = "";
 99    private static String HardwareID = "";
100    private static String Pool = "";
101    private static String sRegString = "";
102
103    private WifiLock wl = null;
104
105    private BroadcastReceiver battReceiver = null;
106
107    private TextView  tv = null;
108
109    public boolean onCreateOptionsMenu(Menu menu)
110        {
111        mExitMenuItem = menu.add("Exit");
112        mExitMenuItem.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
113        return super.onCreateOptionsMenu(menu);
114        }
115
116    public boolean onMenuItemSelected(int featureId, MenuItem item)
117        {
118        if (item == mExitMenuItem)
119            {
120            finish();
121            }
122        return super.onMenuItemSelected(featureId, item);
123        }
124
125    public static String getRegSvrIPAddr()
126        {
127        return(RegSvrIPAddr);
128        }
129
130    /** Called when the activity is first created. */
131    @Override
132    public void onCreate(Bundle savedInstanceState)
133        {
134        super.onCreate(savedInstanceState);
135
136        setContentView(R.layout.main);
137
138        fixScreenOrientation();
139
140        DoCommand dc = new DoCommand(getApplication());
141
142        // Get configuration settings from "ini" file
143        File dir = getFilesDir();
144        File iniFile = new File(dir, "SUTAgent.ini");
145        String sIniFile = iniFile.getAbsolutePath();
146
147        SUTAgentAndroid.RegSvrIPAddr = dc.GetIniData("Registration Server", "IPAddr", sIniFile);
148        SUTAgentAndroid.RegSvrIPPort = dc.GetIniData("Registration Server", "PORT", sIniFile);
149        SUTAgentAndroid.HardwareID = dc.GetIniData("Registration Server", "HARDWARE", sIniFile);
150        SUTAgentAndroid.Pool = dc.GetIniData("Registration Server", "POOL", sIniFile);
151
152        tv = (TextView) this.findViewById(R.id.Textview01);
153
154        if (getLocalIpAddress() == null)
155            setUpNetwork(sIniFile);
156
157        WifiInfo wifi;
158        WifiManager wifiMan = (WifiManager)getSystemService(Context.WIFI_SERVICE);
159        String macAddress = "Unknown";
160        if (wifiMan != null)
161            {
162            wifi = wifiMan.getConnectionInfo();
163            if (wifi != null)
164                {
165                macAddress = wifi.getMacAddress();
166                if (macAddress != null)
167                    sUniqueID = macAddress;
168                }
169            }
170
171        if (sUniqueID == null)
172            {
173            BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
174            if ((ba != null) && (ba.isEnabled() != true))
175                {
176                ba.enable();
177                while(ba.getState() != BluetoothAdapter.STATE_ON)
178                    {
179                    try {
180                        Thread.sleep(1000);
181                        }
182                    catch (InterruptedException e)
183                        {
184                        e.printStackTrace();
185                        }
186                    }
187
188                sUniqueID = ba.getAddress();
189
190                ba.disable();
191                while(ba.getState() != BluetoothAdapter.STATE_OFF)
192                    {
193                    try {
194                        Thread.sleep(1000);
195                        }
196                    catch (InterruptedException e)
197                        {
198                        e.printStackTrace();
199                        }
200                    }
201                }
202            else
203                {
204                if (ba != null)
205                    {
206                    sUniqueID = ba.getAddress();
207                    sUniqueID.toLowerCase();
208                    }
209                }
210            }
211
212        if (sUniqueID == null)
213            {
214            TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
215            if (mTelephonyMgr != null)
216                {
217                sUniqueID = mTelephonyMgr.getDeviceId();
218                if (sUniqueID == null)
219                    {
220                    sUniqueID = "0011223344556677";
221                    }
222                }
223            }
224
225        sLocalIPAddr = getLocalIpAddress();
226        Toast.makeText(getApplication().getApplicationContext(), "SUTAgent [" + sLocalIPAddr + "] ...", Toast.LENGTH_LONG).show();
227
228        String sConfig = "Unique ID: " + sUniqueID + lineSep;
229        sConfig += "OS Info" + lineSep;
230        sConfig += "\t" + dc.GetOSInfo() + lineSep;
231        sConfig += "Screen Info" + lineSep;
232        int [] xy = dc.GetScreenXY();
233        sConfig += "\t Width: " + xy[0] + lineSep;
234        sConfig += "\t Height: " + xy[1] + lineSep;
235        sConfig += "Memory Info" + lineSep;
236        sConfig += "\t" + dc.GetMemoryInfo() + lineSep;
237        sConfig += "Network Info" + lineSep;
238        sConfig += "\tMac Address: " + macAddress + lineSep;
239        sConfig += "\tIP Address: " + sLocalIPAddr + lineSep;
240
241        displayStatus(sConfig);
242
243        sRegString = "NAME=" + sUniqueID;
244        sRegString += "&IPADDR=" + sLocalIPAddr;
245        sRegString += "&CMDPORT=" + 20701;
246        sRegString += "&DATAPORT=" + 20700;
247        sRegString += "&OS=Android-" + dc.GetOSInfo();
248        sRegString += "&SCRNWIDTH=" + xy[0];
249        sRegString += "&SCRNHEIGHT=" + xy[1];
250        sRegString += "&BPP=8";
251        sRegString += "&MEMORY=" + dc.GetMemoryConfig();
252        sRegString += "&HARDWARE=" + HardwareID;
253        sRegString += "&POOL=" + Pool;
254
255        String sTemp = Uri.encode(sRegString,"=&");
256        sRegString = "register " + sTemp;
257
258        if (!bNetworkingStarted)
259            {
260            Thread thread = new Thread(null, doStartService, "StartServiceBkgnd");
261            thread.start();
262            bNetworkingStarted = true;
263
264            Thread thread2 = new Thread(null, doRegisterDevice, "RegisterDeviceBkgnd");
265            thread2.start();
266            }
267
268        monitorBatteryState();
269
270        // If we are returning from an update let'em know we're back
271        Thread thread3 = new Thread(null, doUpdateCallback, "UpdateCallbackBkgnd");
272        thread3.start();
273
274        final Button goButton = (Button) findViewById(R.id.Button01);
275        goButton.setOnClickListener(new OnClickListener() {
276            public void onClick(View v) {
277                finish();
278                }
279            });
280        }
281
282    private class UpdateStatus implements Runnable {
283        public String sText = "";
284
285        UpdateStatus(String sStatus) {
286            sText = sStatus;
287        }
288
289        public void run() {
290            displayStatus(sText);
291        }
292    }
293
294    public synchronized void displayStatus(String sStatus) {
295        String sTVText = (String) tv.getText();
296        sTVText += sStatus;
297        tv.setText(sTVText);
298    }
299
300    public void fixScreenOrientation()
301        {
302        setRequestedOrientation((getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) ?
303                                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
304        }
305
306    protected void onActivityResult(int requestCode, int resultCode, Intent data)
307        {
308        if (requestCode == START_PRG)
309            {
310               Toast.makeText(getApplication().getApplicationContext(), "SUTAgent startprg finished ...", Toast.LENGTH_LONG).show();
311            }
312        }
313
314    @Override
315    public void onDestroy()
316        {
317        super.onDestroy();
318        if (isFinishing())
319            {
320            Intent listenerSvc = new Intent(this, ASMozStub.class);
321            listenerSvc.setAction("com.mozilla.SUTAgentAndroid.service.LISTENER_SERVICE");
322            stopService(listenerSvc);
323            bNetworkingStarted = false;
324
325            unregisterReceiver(battReceiver);
326
327            if (wl != null)
328                wl.release();
329
330            System.exit(0);
331            }
332        }
333
334    private void monitorBatteryState()
335        {
336        battReceiver = new BroadcastReceiver()
337            {
338            public void onReceive(Context context, Intent intent)
339                {
340                StringBuilder sb = new StringBuilder();
341
342                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); // charge level from 0 to scale inclusive
343                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); // Max value for charge level
344                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
345                int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
346                boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
347                int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); //0 if the device is not plugged in; 1 if plugged into an AC power adapter; 2 if plugged in via USB.
348//                int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); // voltage in millivolts
349                nBatteryTemp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); // current battery temperature in tenths of a degree Centigrade
350//                String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
351
352                nChargeLevel = -1;  // percentage, or -1 for unknown
353                if (rawlevel >= 0 && scale > 0)
354                    {
355                    nChargeLevel = (rawlevel * 100) / scale;
356                    }
357
358                if (plugged > 0)
359                    sACStatus = "ONLINE";
360                else
361                    sACStatus = "OFFLINE";
362
363                if (present == false)
364                    sb.append("NO BATTERY");
365                else
366                    {
367                    if (nChargeLevel < 10)
368                        sb.append("Critical");
369                    else if (nChargeLevel < 33)
370                        sb.append("LOW");
371                    else if (nChargeLevel > 80)
372                        sb.append("HIGH");
373                    }
374
375                if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health)
376                    {
377                    sb.append("Overheated ");
378                    sb.append((((float)(nBatteryTemp))/10));
379                    sb.append("(C)");
380                    }
381                else
382                    {
383                    switch(status)
384                        {
385                        case BatteryManager.BATTERY_STATUS_UNKNOWN:
386                            // old emulator; maybe also when plugged in with no battery
387                            if (present == true)
388                                sb.append(" UNKNOWN");
389                            break;
390                        case BatteryManager.BATTERY_STATUS_CHARGING:
391                            sb.append(" CHARGING");
392                            break;
393                        case BatteryManager.BATTERY_STATUS_DISCHARGING:
394                            sb.append(" DISCHARGING");
395                            break;
396                        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
397                            sb.append(" NOTCHARGING");
398                            break;
399                        case BatteryManager.BATTERY_STATUS_FULL:
400                            sb.append(" FULL");
401                            break;
402                        default:
403                            if (present == true)
404                                sb.append("Unknown");
405                            break;
406                        }
407                    }
408
409                sPowerStatus = sb.toString();
410                }
411            };
412
413        IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
414        registerReceiver(battReceiver, battFilter);
415        }
416
417    public boolean setUpNetwork(String sIniFile)
418        {
419        boolean    bRet = false;
420        int    lcv    = 0;
421        int    lcv2 = 0;
422        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
423        WifiConfiguration wc = new WifiConfiguration();
424        DoCommand tmpdc = new DoCommand(getApplication());
425
426        String ssid = tmpdc.GetIniData("Network Settings", "SSID", sIniFile);
427        String auth = tmpdc.GetIniData("Network Settings", "AUTH", sIniFile);
428        String encr = tmpdc.GetIniData("Network Settings", "ENCR", sIniFile);
429        String key = tmpdc.GetIniData("Network Settings", "KEY", sIniFile);
430        String eap = tmpdc.GetIniData("Network Settings", "EAP", sIniFile);
431        String adhoc = tmpdc.GetIniData("Network Settings", "ADHOC", sIniFile);
432
433        Toast.makeText(getApplication().getApplicationContext(), "Starting and configuring network", Toast.LENGTH_LONG).show();
434/*
435        ContentResolver cr = getContentResolver();
436        int nRet;
437        try {
438            nRet = Settings.System.getInt(cr, Settings.System.WIFI_USE_STATIC_IP);
439            String foo2 = "" + nRet;
440        } catch (SettingNotFoundException e1) {
441            e1.printStackTrace();
442        }
443*/
444/*
445        wc.SSID = "\"Mozilla-Build\"";
446        wc.preSharedKey  = "\"MozillaBuildQA500\"";
447        wc.hiddenSSID = true;
448        wc.status = WifiConfiguration.Status.ENABLED;
449        wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
450        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
451        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
452        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
453        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
454        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
455        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
456*/
457        wc.SSID = "\"" + ssid + "\"";
458//        wc.SSID = "\"Mozilla-G\"";
459//        wc.SSID = "\"Mozilla\"";
460
461        if (auth.contentEquals("wpa2"))
462            {
463            wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
464            wc.preSharedKey  = null;
465            }
466
467        if (encr.contentEquals("aes"))
468            {
469            wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
470            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
471            }
472
473        if (eap.contentEquals("peap"))
474            {
475            wc.eap.setValue("PEAP");
476            wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
477            wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
478            }
479
480        wc.hiddenSSID = false;
481        wc.status = WifiConfiguration.Status.ENABLED;
482
483        wc.password.setValue("\"password\"");
484        wc.identity.setValue("\"bmoss@mozilla.com\"");
485
486        if (!wifi.isWifiEnabled())
487            wifi.setWifiEnabled(true);
488
489        while(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED)
490            {
491            Thread.yield();
492            if (++lcv > 10000)
493                return(bRet);
494            }
495
496        wl = wifi.createWifiLock(WifiManager.WIFI_MODE_FULL, "SUTAgent");
497        if (wl != null)
498            wl.acquire();
499
500        WifiConfiguration    foo = null;
501        int                    nNetworkID = -1;
502
503        List<WifiConfiguration> connsLst =  wifi.getConfiguredNetworks();
504        int nConns = connsLst.size();
505        for (int i = 0; i < nConns; i++)
506            {
507
508            foo = connsLst.get(i);
509            if (foo.SSID.equalsIgnoreCase(wc.SSID))
510                {
511                nNetworkID = foo.networkId;
512                wc.networkId = foo.networkId;
513                break;
514                }
515            }
516
517        int res;
518
519        if (nNetworkID != -1)
520            {
521            res = wifi.updateNetwork(wc);
522            }
523        else
524            {
525            res = wifi.addNetwork(wc);
526            }
527
528        Log.d("WifiPreference", "add Network returned " + res );
529
530        boolean b = wifi.enableNetwork(res, true);
531        Log.d("WifiPreference", "enableNetwork returned " + b );
532
533        wifi.saveConfiguration();
534
535        WifiInfo wi = wifi.getConnectionInfo();
536        SupplicantState ss = wi.getSupplicantState();
537
538        lcv = 0;
539        lcv2 = 0;
540
541        while (ss.compareTo(SupplicantState.COMPLETED) != 0)
542            {
543            try {
544                Thread.sleep(1000);
545                }
546            catch (InterruptedException e)
547                {
548                e.printStackTrace();
549                }
550
551            if (wi != null)
552                wi = null;
553            if (ss != null)
554                ss = null;
555            wi = wifi.getConnectionInfo();
556            ss = wi.getSupplicantState();
557            if (++lcv > 60)
558                {
559                if (++lcv2 > 5)
560                    {
561                    Toast.makeText(getApplication().getApplicationContext(), "Unable to start and configure network", Toast.LENGTH_LONG).show();
562                    return(bRet);
563                    }
564                else
565                    {
566                    Toast.makeText(getApplication().getApplicationContext(), "Resetting wifi interface", Toast.LENGTH_LONG).show();
567                    if (wl != null)
568                        wl.release();
569                    wifi.setWifiEnabled(false);
570                    while(wifi.getWifiState() != WifiManager.WIFI_STATE_DISABLED)
571                        {
572                        Thread.yield();
573                        }
574
575                    wifi.setWifiEnabled(true);
576                    while(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED)
577                        {
578                        Thread.yield();
579                        }
580                    b = wifi.enableNetwork(res, true);
581                    Log.d("WifiPreference", "enableNetwork returned " + b );
582                    if (wl != null)
583                        wl.acquire();
584                    lcv = 0;
585                    }
586                }
587            }
588
589        lcv = 0;
590        while(getLocalIpAddress() == null)
591            {
592            if (++lcv > 10000)
593                return(bRet);
594            }
595
596        Toast.makeText(getApplication().getApplicationContext(), "Network started and configured", Toast.LENGTH_LONG).show();
597        bRet = true;
598
599        return(bRet);
600        }
601
602    // If there is an update.info file callback the server and send the status
603    private Runnable doUpdateCallback = new Runnable() {
604        public void run() {
605            DoCommand dc = new DoCommand(getApplication());
606            String sRet = dc.UpdateCallBack("update.info");
607            if (sRet.length() > 0) {
608                if (sRet.contains("ok")) {
609                    sRet = "Callback Server contacted successfully" + lineSep;
610                } else if (sRet.contains("Nothing to do")) {
611                    sRet = "";
612                } else {
613                    sRet = "Callback Server NOT contacted successfully" + lineSep;
614                }
615            }
616            if (sRet.length() > 0)
617                mHandler.post(new UpdateStatus(sRet));
618            dc = null;
619        }
620    };
621
622    // registers with the reg server defined in the SUTAgent.ini file
623    private Runnable doRegisterDevice = new Runnable() {
624        public void run() {
625            DoCommand dc = new DoCommand(getApplication());
626            String sRet = "";
627            if (RegSvrIPAddr.length() > 0) {
628                String sRegRet = dc.RegisterTheDevice(RegSvrIPAddr, RegSvrIPPort, sRegString);
629                if (sRegRet.contains("ok")) {
630                    sRet += "Registered with testserver" + lineSep;
631                    sRet += "\tIPAddress: " + RegSvrIPAddr + lineSep;
632                    if (RegSvrIPPort.length() > 0)
633                        sRet += "\tPort: " + RegSvrIPPort + lineSep;
634                } else {
635                    sRet += "Not registered with testserver" + lineSep;
636                }
637            } else {
638                sRet += "Not registered with testserver" + lineSep;
639            }
640
641        if (sRet.length() > 0)
642            mHandler.post(new UpdateStatus(sRet));
643        dc = null;
644        }
645    };
646
647    // this starts the listener service for the command and data channels
648    private Runnable doStartService = new Runnable()
649        {
650        public void run()
651            {
652            Intent listenerService = new Intent();
653            listenerService.setAction("com.mozilla.SUTAgentAndroid.service.LISTENER_SERVICE");
654            startService(listenerService);
655            }
656        };
657
658    public String getLocalIpAddress()
659        {
660        try
661            {
662            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
663                {
664                NetworkInterface intf = en.nextElement();
665                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
666                    {
667                    InetAddress inetAddress = enumIpAddr.nextElement();
668                    if (!inetAddress.isLoopbackAddress())
669                        {
670                        return inetAddress.getHostAddress().toString();
671                        }
672                    }
673                }
674            }
675        catch (SocketException ex)
676            {
677            Toast.makeText(getApplication().getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
678            }
679        return null;
680        }
681}