2011-10-29 11:56:13 +01:00
2010-09-27 11:03:40 +01:00
package org.onaips.vnc ;
2010-09-16 02:39:31 +01:00
import java.io.BufferedReader ;
import java.io.File ;
import java.io.FileReader ;
import java.io.IOException ;
import java.io.OutputStream ;
import java.net.InetAddress ;
import java.net.NetworkInterface ;
import java.net.SocketException ;
import java.util.Enumeration ;
import java.util.List ;
import java.util.Timer ;
import java.util.TimerTask ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import android.app.Activity ;
import android.app.AlertDialog ;
import android.content.BroadcastReceiver ;
2011-10-29 11:56:13 +01:00
import android.content.ComponentName ;
2010-09-16 02:39:31 +01:00
import android.content.Context ;
import android.content.DialogInterface ;
import android.content.Intent ;
import android.content.IntentFilter ;
2011-10-29 11:56:13 +01:00
import android.content.ServiceConnection ;
2010-09-16 02:39:31 +01:00
import android.content.SharedPreferences ;
2011-10-29 11:56:13 +01:00
import android.content.SharedPreferences.Editor ;
2010-09-16 02:39:31 +01:00
import android.content.pm.PackageInfo ;
import android.content.pm.PackageManager ;
import android.content.pm.PackageManager.NameNotFoundException ;
2012-06-11 20:08:48 +01:00
import android.content.pm.ResolveInfo ;
2010-09-16 02:39:31 +01:00
import android.graphics.Color ;
import android.net.ConnectivityManager ;
import android.net.NetworkInfo ;
2011-10-29 11:56:13 +01:00
import android.net.Uri ;
2010-09-16 02:39:31 +01:00
import android.os.Build ;
import android.os.Bundle ;
2011-10-29 11:56:13 +01:00
import android.os.IBinder ;
2010-09-16 02:39:31 +01:00
import android.preference.PreferenceManager ;
import android.text.Html ;
import android.util.Log ;
import android.view.Menu ;
import android.view.MenuInflater ;
import android.view.MenuItem ;
import android.view.View ;
import android.view.View.OnClickListener ;
2012-06-11 20:08:48 +01:00
import android.view.Window ;
2011-10-29 11:56:13 +01:00
import android.view.animation.Animation ;
import android.view.animation.Animation.AnimationListener ;
2012-06-11 20:08:48 +01:00
import android.view.animation.AnimationUtils ;
2010-09-16 02:39:31 +01:00
import android.widget.Button ;
2011-10-29 11:56:13 +01:00
import android.widget.EditText ;
2012-06-11 20:08:48 +01:00
import android.widget.RelativeLayout ;
2010-09-16 02:39:31 +01:00
import android.widget.TextView ;
import android.widget.Toast ;
2012-06-11 20:08:48 +01:00
import com.google.ads.AdRequest ;
import com.google.ads.AdSize ;
import com.google.ads.AdView ;
2010-09-16 02:39:31 +01:00
public class MainActivity extends Activity
2010-09-27 11:03:40 +01:00
{
2010-09-16 02:39:31 +01:00
private static final int MENU_QUIT = 0 ;
private static final int MENU_HELP = 1 ;
private static final int MENU_ONAIPS = 2 ;
2011-10-29 11:56:13 +01:00
private static final int MENU_SENDLOG = 3 ;
private static final int MENU_REVERSE_CONNECTION = 4 ;
2010-12-25 15:33:52 +00:00
2011-10-29 11:56:13 +01:00
static final int APP_ID = 123 ;
static final String VNC_LOG = " VNCserver " ;
2012-06-11 20:08:48 +01:00
private AdView adView = null ;
private ServerManager s = null ;
private Animation buttonAnimation = null ;
private SharedPreferences preferences ;
private AlertDialog startDialog ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
void doBindService ( ) {
bindService ( new Intent ( this , ServerManager . class ) , mConnection ,
Context . BIND_AUTO_CREATE ) ;
}
@Override
public void onResume ( )
{
IntentFilter i ;
i = new IntentFilter ( " org.onaips.vnc.ACTIVITY_UPDATE " ) ;
ActivityUpdateReceiver receiver = new ActivityUpdateReceiver ( ) ;
registerReceiver ( receiver , i ) ;
2010-12-25 15:33:52 +00:00
2011-10-29 11:56:13 +01:00
super . onResume ( ) ;
}
public class ActivityUpdateReceiver extends BroadcastReceiver
{
@Override
public void onReceive ( Context context , Intent intent ) //this method receives broadcast messages. Be sure to modify AndroidManifest.xml file in order to enable message receiving
{
try {
Thread . sleep ( 2000 ) ;
} catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
setStateLabels ( ServerManager . isServerRunning ( ) ) ;
}
}
private ServiceConnection mConnection = new ServiceConnection ( ) {
public void onServiceConnected ( ComponentName className , IBinder binder ) {
s = ( ( ServerManager . MyBinder ) binder ) . getService ( ) ;
}
public void onServiceDisconnected ( ComponentName className ) {
s = null ;
}
} ;
@Override
2010-12-14 22:20:30 +00:00
protected void onDestroy ( )
{
2012-06-11 20:08:48 +01:00
if ( adView ! = null )
adView . destroy ( ) ;
2010-12-14 22:20:30 +00:00
super . onDestroy ( ) ;
unregisterReceiver ( mReceiver ) ;
}
2011-10-29 11:56:13 +01:00
//rodar vnc com acc
2012-06-11 20:08:48 +01:00
2010-09-16 02:39:31 +01:00
/** Called when the activity is first created. */
@Override
public void onCreate ( Bundle savedInstanceState )
{
2011-10-29 11:56:13 +01:00
super . onCreate ( savedInstanceState ) ;
requestWindowFeature ( Window . FEATURE_NO_TITLE ) ;
2010-09-16 02:39:31 +01:00
setContentView ( R . layout . main ) ;
2011-10-29 11:56:13 +01:00
doBindService ( ) ;
2010-09-16 02:39:31 +01:00
2010-12-14 22:20:30 +00:00
2010-09-16 02:39:31 +01:00
// Initialize preferences
preferences = PreferenceManager . getDefaultSharedPreferences ( this ) ;
2011-10-29 11:56:13 +01:00
boolean root = preferences . getBoolean ( " asroot " , true ) ;
if ( ! hasRootPermission ( ) & & root )
2010-09-16 02:39:31 +01:00
{
2011-10-29 11:56:13 +01:00
log ( " You do not have root permissions...!!! " ) ;
2010-12-14 22:20:30 +00:00
startDialog = new AlertDialog . Builder ( this ) . create ( ) ;
startDialog . setTitle ( " Cannot continue " ) ;
2011-10-29 11:56:13 +01:00
startDialog . setMessage ( " You do not have root permissions. \ nPlease root your phone first! \ n \ nDo you want to continue anyway? " ) ;
2010-12-14 22:20:30 +00:00
startDialog . setIcon ( R . drawable . icon ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
startDialog . setButton ( " Yes " , new DialogInterface . OnClickListener ( ) {
2010-12-14 22:20:30 +00:00
public void onClick ( DialogInterface arg0 , int arg1 ) {
2011-10-29 11:56:13 +01:00
Editor e = preferences . edit ( ) ;
e . putBoolean ( " asroot " , false ) ;
e . commit ( ) ;
2010-12-14 22:20:30 +00:00
startDialog . dismiss ( ) ;
}
} ) ;
startDialog . setButton2 ( " No " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
System . exit ( 0 ) ;
}
} ) ;
startDialog . show ( ) ;
2010-09-16 02:39:31 +01:00
}
2010-12-14 22:20:30 +00:00
showInitialScreen ( false ) ;
2011-10-29 11:56:13 +01:00
setStateLabels ( ServerManager . isServerRunning ( ) ) ;
2010-09-16 02:39:31 +01:00
// register wifi event receiver
2010-12-14 22:20:30 +00:00
registerReceiver ( mReceiver , new IntentFilter ( ConnectivityManager . CONNECTIVITY_ACTION ) ) ;
2010-12-25 15:33:52 +00:00
2011-10-29 11:56:13 +01:00
boolean hidead = preferences . getBoolean ( " hidead " , false ) ;
if ( ! hidead )
{
2010-09-27 11:03:40 +01:00
2011-10-29 11:56:13 +01:00
// Look up the AdView as a resource and load a request.
2012-06-11 20:08:48 +01:00
adView = ( AdView ) this . findViewById ( R . id . adView ) ;
2011-10-29 11:56:13 +01:00
adView . loadAd ( new AdRequest ( ) ) ;
}
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
findViewById ( R . id . Button01 ) . setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View arg0 ) {
final Button b = ( Button ) findViewById ( R . id . Button01 ) ;
buttonAnimation = AnimationUtils . loadAnimation ( MainActivity . this , R . anim . animation ) ;
buttonAnimation . setAnimationListener ( new AnimationListener ( ) {
public void onAnimationEnd ( Animation animation ) {
b . setEnabled ( true ) ;
//b.setVisibility(View.INVISIBLE);
}
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
public void onAnimationRepeat ( Animation animation ) {
}
2010-12-14 22:20:30 +00:00
2011-10-29 11:56:13 +01:00
public void onAnimationStart ( Animation animation ) {
b . setEnabled ( false ) ;
2010-12-14 22:20:30 +00:00
2011-10-29 11:56:13 +01:00
if ( ServerManager . isServerRunning ( ) )
stopServer ( ) ;
else
startServer ( ) ;
}
} ) ;
b . startAnimation ( buttonAnimation ) ;
2010-12-14 22:20:30 +00:00
2010-09-16 02:39:31 +01:00
return ;
}
} ) ;
findViewById ( R . id . Button02 ) . setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View arg0 ) {
2011-10-29 11:56:13 +01:00
restartServer ( ) ;
2010-09-16 02:39:31 +01:00
return ;
}
} ) ;
}
2012-06-11 20:08:48 +01:00
2011-10-29 11:56:13 +01:00
public void log ( String s )
2010-12-14 22:20:30 +00:00
{
2011-10-29 11:56:13 +01:00
Log . v ( VNC_LOG , s ) ;
2010-12-14 22:20:30 +00:00
}
2010-09-16 02:39:31 +01:00
public String packageVersion ( )
{
String version = " " ;
try {
PackageInfo pi = getPackageManager ( ) . getPackageInfo ( getPackageName ( ) , 0 ) ;
version = pi . versionName ;
} catch ( NameNotFoundException e ) {
2011-10-29 11:56:13 +01:00
log ( " onOptionsItemSelected: " + e . getMessage ( ) ) ;
2010-09-16 02:39:31 +01:00
} ;
return version ;
}
public void showInitialScreen ( boolean forceShow )
{
// Initialize preferences
preferences = PreferenceManager . getDefaultSharedPreferences ( this ) ;
SharedPreferences . Editor editor = preferences . edit ( ) ;
String version = packageVersion ( ) ;
2010-09-24 20:47:56 +01:00
if ( ( ! forceShow ) & & ( version . equals ( preferences . getString ( " version " , " " ) ) ) )
return ;
editor . putString ( " version " , version ) ;
editor . commit ( ) ;
2010-09-16 02:39:31 +01:00
startDialog = new AlertDialog . Builder ( this ) . create ( ) ;
2011-10-29 11:56:13 +01:00
startDialog . setTitle ( " droid VNC server " ) ;
startDialog . setMessage ( Html . fromHtml ( " Welcome to droid VNC server version " + version + " .<br>This is beta software so please provide some feedback about your experience!<br><br>Best Regards, @oNaiPs " ) ) ;
2010-09-16 02:39:31 +01:00
startDialog . setIcon ( R . drawable . icon ) ;
2010-12-25 15:33:52 +00:00
startDialog . setButton ( AlertDialog . BUTTON1 , " OK " , new DialogInterface . OnClickListener ( ) {
2010-09-16 02:39:31 +01:00
2010-12-25 15:33:52 +00:00
public void onClick ( DialogInterface arg0 , int arg1 ) {
startDialog . dismiss ( ) ;
}
} ) ;
2010-09-16 02:39:31 +01:00
2010-12-25 15:33:52 +00:00
startDialog . setButton2 ( " Donate " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
Intent myIntent = new Intent ( Intent . ACTION_VIEW , Uri . parse ( " market://details?id=org.onaips.donate " ) ) ;
startActivity ( myIntent ) ;
}
} ) ;
startDialog . show ( ) ;
2010-09-16 02:39:31 +01:00
}
public void showTextOnScreen ( final String t )
{
runOnUiThread ( new Runnable ( ) {
public void run ( ) {
Toast . makeText ( MainActivity . this , t , Toast . LENGTH_LONG ) . show ( ) ;
}
} ) ;
}
public boolean onCreateOptionsMenu ( Menu menu ) {
MenuInflater inflater = getMenuInflater ( ) ;
inflater . inflate ( R . menu . menu , menu ) ;
2010-09-24 20:47:56 +01:00
menu . add ( 0 , MENU_SENDLOG , 0 , " Report issue " ) ;
menu . add ( 0 , MENU_ONAIPS , 0 , " About " ) ;
menu . add ( 0 , MENU_HELP , 0 , " Help " ) ;
2011-10-29 11:56:13 +01:00
menu . add ( 0 , MENU_REVERSE_CONNECTION , 0 , " Reverse \ nConnection " ) ;
2010-09-24 20:47:56 +01:00
menu . add ( 0 , MENU_QUIT , 0 , " Close " ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
return true ;
2010-09-16 02:39:31 +01:00
}
public void setStateLabels ( boolean state )
{
2011-10-29 11:56:13 +01:00
2010-09-16 02:39:31 +01:00
TextView stateLabel = ( TextView ) findViewById ( R . id . stateLabel ) ;
stateLabel . setText ( state ? " Running " : " Stopped " ) ;
2011-10-29 11:56:13 +01:00
stateLabel . setTextColor ( state ? Color . rgb ( 114 , 182 , 43 ) : Color . rgb ( 234 , 113 , 29 ) ) ;
TextView t = ( TextView ) findViewById ( R . id . TextView01 ) ;
Button b = ( Button ) findViewById ( R . id . Button01 ) ;
b . clearAnimation ( ) ;
Button b2 = ( Button ) findViewById ( R . id . Button02 ) ;
2010-09-16 02:39:31 +01:00
if ( state )
{
String port = preferences . getString ( " port " , " 5901 " ) ;
String httpport ;
try
{
int port1 = Integer . parseInt ( port ) ;
port = String . valueOf ( port1 ) ;
httpport = String . valueOf ( port1 - 100 ) ;
}
catch ( NumberFormatException e )
{
port = " 5901 " ;
httpport = " 5801 " ;
}
2010-09-24 20:47:56 +01:00
2010-09-16 02:39:31 +01:00
String ip = getIpAddress ( ) ;
if ( ip . equals ( " " ) )
2010-09-24 20:47:56 +01:00
t . setText ( Html . fromHtml ( " Not connected to a network.<br> You can connect through USB with:<br>localhost: " + port + " <br>or<br>http://localhost: " + httpport + " <br>(use adb to forward ports)</font> " ) ) ;
2010-09-16 02:39:31 +01:00
else
2010-09-24 20:47:56 +01:00
t . setText ( Html . fromHtml ( " <font align= \" center \" >Connect to:<br> " + ip + " : " + port + " <br>or<br>http:// " + ip + " : " + httpport + " </font> " ) ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
b . setBackgroundDrawable ( getResources ( ) . getDrawable ( R . drawable . btnstop_normal ) ) ;
b2 . setVisibility ( View . VISIBLE ) ;
2010-09-27 11:03:40 +01:00
2010-09-16 02:39:31 +01:00
}
else
2011-10-29 11:56:13 +01:00
{
2010-09-16 02:39:31 +01:00
t . setText ( " " ) ;
2011-10-29 11:56:13 +01:00
b . setBackgroundDrawable ( getResources ( ) . getDrawable ( R . drawable . btnstart_normal ) ) ;
b2 . setVisibility ( View . INVISIBLE ) ;
}
2010-09-16 02:39:31 +01:00
}
public String getIpAddress ( ) {
try {
for ( Enumeration < NetworkInterface > en = NetworkInterface . getNetworkInterfaces ( ) ; en . hasMoreElements ( ) ; ) {
NetworkInterface intf = en . nextElement ( ) ;
for ( Enumeration < InetAddress > enumIpAddr = intf . getInetAddresses ( ) ; enumIpAddr . hasMoreElements ( ) ; ) {
InetAddress inetAddress = enumIpAddr . nextElement ( ) ;
if ( ! inetAddress . isLoopbackAddress ( ) ) {
return inetAddress . getHostAddress ( ) . toString ( ) ;
}
}
}
} catch ( SocketException ex ) {
2011-10-29 11:56:13 +01:00
log ( ex . toString ( ) ) ;
2010-09-16 02:39:31 +01:00
}
return " " ;
}
2011-10-29 11:56:13 +01:00
public void restartServer ( )
2010-09-16 02:39:31 +01:00
{
2011-10-29 11:56:13 +01:00
startDialog = new AlertDialog . Builder ( this ) . create ( ) ;
startDialog . setTitle ( " Already running " ) ;
startDialog . setMessage ( " Restart server? " ) ;
startDialog . setButton ( AlertDialog . BUTTON1 , " Yes " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
stopServer ( ) ;
try {
Thread . sleep ( 2000 ) ;
} catch ( InterruptedException e ) {
2010-12-14 22:20:30 +00:00
}
2011-10-29 11:56:13 +01:00
startServer ( ) ;
2010-09-16 02:39:31 +01:00
}
2011-10-29 11:56:13 +01:00
} ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
startDialog . setButton2 ( " No " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
}
} ) ;
startDialog . show ( ) ;
2010-09-16 02:39:31 +01:00
}
public void startServer ( )
{
2011-10-29 11:56:13 +01:00
s . startServer ( ) ;
Timer t = new Timer ( ) ;
t . schedule ( new TimerTask ( ) {
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
@Override
public void run ( ) {
if ( ! ServerManager . isServerRunning ( ) )
{
runOnUiThread ( new Runnable ( ) {
2010-12-25 15:33:52 +00:00
2011-10-29 11:56:13 +01:00
public void run ( ) {
showTextOnScreen ( " Could not start server :( " ) ;
log ( " Could not start server :( " ) ;
setStateLabels ( ServerManager . isServerRunning ( ) ) ;
}
} ) ;
}
2010-09-16 02:39:31 +01:00
}
2011-10-29 11:56:13 +01:00
} , 2000 ) ;
}
public void stopServer ( )
{
s . killServer ( ) ;
Timer t = new Timer ( ) ;
t . schedule ( new TimerTask ( ) {
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
@Override
public void run ( ) {
if ( ServerManager . isServerRunning ( ) )
{
runOnUiThread ( new Runnable ( ) {
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
public void run ( ) {
showTextOnScreen ( " Could not stop server :( " ) ;
log ( " Could not stop server :( " ) ;
setStateLabels ( ServerManager . isServerRunning ( ) ) ;
}
} ) ;
}
}
} , 4000 ) ;
2010-09-16 02:39:31 +01:00
}
public void showHelp ( )
{
new AlertDialog . Builder ( this )
. setTitle ( " Help " )
. setMessage ( Html . fromHtml ( " Mouse Mappings:<br><br>Right Click -> Back<br>Middle Click -> End Call<br>Left Click -> Touch<br><br>Keyboard Mappings<br><br> " +
" Home Key -> Home<br>Escape -> Back<br>Page Up ->Menu<br>Left Ctrl -> Search<br>PgDown -> Start Call<br> " +
2011-10-29 11:56:13 +01:00
" End Key -> End Call<br>F4 -> Rotate<br>F11 -> Disconnect<br>F12 -> Stop Server Daemon " ) )
2010-09-24 20:47:56 +01:00
. setPositiveButton ( " Quit " , null )
2010-09-16 02:39:31 +01:00
. setNegativeButton ( " Open Website " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
2011-10-29 11:56:13 +01:00
Intent myIntent = new Intent ( Intent . ACTION_VIEW , Uri . parse ( " http://www.onaips.com " ) ) ;
2010-09-16 02:39:31 +01:00
startActivity ( myIntent ) ;
}
} )
. show ( ) ;
}
2011-10-29 11:56:13 +01:00
2010-09-16 02:39:31 +01:00
// This method is called once the menu is selected
public boolean onOptionsItemSelected ( MenuItem item ) {
switch ( item . getItemId ( ) ) {
// We have only one menu option
case R . id . preferences :
// Launch Preference activity
Intent i = new Intent ( MainActivity . this , preferences . class ) ;
startActivity ( i ) ;
showTextOnScreen ( " Don't forget to stop/start the server after changes " ) ;
break ;
case MENU_QUIT :
System . exit ( 1 ) ;
break ;
case MENU_HELP :
showHelp ( ) ;
2011-10-29 11:56:13 +01:00
break ;
case MENU_REVERSE_CONNECTION :
if ( ServerManager . isServerRunning ( ) )
{
startDialog = new AlertDialog . Builder ( this ) . create ( ) ;
startDialog . setTitle ( " Already running " ) ;
startDialog . setMessage ( " Restart server? " ) ;
startDialog . setButton ( AlertDialog . BUTTON1 , " Yes " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
startReverseConnection ( ) ;
}
} ) ;
startDialog . setButton2 ( " Cancel " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
}
} ) ;
startDialog . show ( ) ;
}
else
startReverseConnection ( ) ;
2010-09-16 02:39:31 +01:00
break ;
case MENU_SENDLOG :
collectAndSendLog ( ) ;
break ;
case MENU_ONAIPS :
new AlertDialog . Builder ( this )
. setTitle ( " About " )
2011-10-29 11:56:13 +01:00
. setMessage ( Html . fromHtml ( " version " + packageVersion ( ) + " <br><br>Code: @oNaiPs<br><br>Graphics: ricardomendes.net " ) )
2010-09-16 02:39:31 +01:00
. setPositiveButton ( " Close " , null )
. setNegativeButton ( " Open Website " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface arg0 , int arg1 ) {
2011-10-29 11:56:13 +01:00
Intent myIntent = new Intent ( Intent . ACTION_VIEW , Uri . parse ( " http://onaips.com " ) ) ;
2010-09-16 02:39:31 +01:00
startActivity ( myIntent ) ;
}
} )
. show ( ) ;
2011-10-29 11:56:13 +01:00
break ;
2010-09-16 02:39:31 +01:00
}
2011-10-29 11:56:13 +01:00
return true ;
2010-09-16 02:39:31 +01:00
}
2011-10-29 11:56:13 +01:00
public void startReverseConnection ( )
2010-09-16 02:39:31 +01:00
{
2010-12-25 15:33:52 +00:00
2011-10-29 11:56:13 +01:00
AlertDialog . Builder alert = new AlertDialog . Builder ( this ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
alert . setTitle ( " Reverse Connection " ) ;
alert . setMessage ( " Input <host:port>: " ) ;
2010-12-14 22:20:30 +00:00
2011-10-29 11:56:13 +01:00
// Set an EditText view to get user input
final EditText input = new EditText ( this ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
alert . setView ( input ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
alert . setPositiveButton ( " Start server " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int whichButton ) {
s . startReverseConnection ( input . getText ( ) . toString ( ) ) ;
}
} ) ;
2010-09-24 20:47:56 +01:00
2011-10-29 11:56:13 +01:00
alert . setNegativeButton ( " Cancel " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int whichButton ) {
// Canceled.
}
} ) ;
2010-09-16 02:39:31 +01:00
2011-10-29 11:56:13 +01:00
alert . show ( ) ;
}
public static boolean hasRootPermission ( ) {
2010-09-16 02:39:31 +01:00
boolean rooted = true ;
try {
File su = new File ( " /system/bin/su " ) ;
if ( su . exists ( ) = = false ) {
su = new File ( " /system/xbin/su " ) ;
if ( su . exists ( ) = = false ) {
rooted = false ;
}
}
} catch ( Exception e ) {
2011-10-29 11:56:13 +01:00
//log( "Can't obtain root - Here is what I know: "+e.getMessage());
2010-09-16 02:39:31 +01:00
rooted = false ;
}
return rooted ;
}
2011-10-29 11:56:13 +01:00
public static final String LOG_COLLECTOR_PACKAGE_NAME = " com.xtralogic.android.logcollector " ;
public static final String ACTION_SEND_LOG = " com.xtralogic.logcollector.intent.action.SEND_LOG " ;
public static final String EXTRA_SEND_INTENT_ACTION = " com.xtralogic.logcollector.intent.extra.SEND_INTENT_ACTION " ;
public static final String EXTRA_DATA = " com.xtralogic.logcollector.intent.extra.DATA " ;
public static final String EXTRA_ADDITIONAL_INFO = " com.xtralogic.logcollector.intent.extra.ADDITIONAL_INFO " ;
public static final String EXTRA_SHOW_UI = " com.xtralogic.logcollector.intent.extra.SHOW_UI " ;
public static final String EXTRA_FILTER_SPECS = " com.xtralogic.logcollector.intent.extra.FILTER_SPECS " ;
public static final String EXTRA_FORMAT = " com.xtralogic.logcollector.intent.extra.FORMAT " ;
public static final String EXTRA_BUFFER = " com.xtralogic.logcollector.intent.extra.BUFFER " ;
2010-09-16 02:39:31 +01:00
void collectAndSendLog ( ) {
final PackageManager packageManager = getPackageManager ( ) ;
final Intent intent = new Intent ( ACTION_SEND_LOG ) ;
List < ResolveInfo > list = packageManager . queryIntentActivities ( intent , PackageManager . MATCH_DEFAULT_ONLY ) ;
final boolean isInstalled = list . size ( ) > 0 ;
if ( ! isInstalled ) {
new AlertDialog . Builder ( this )
. setTitle ( getString ( R . string . app_name ) )
. setIcon ( android . R . drawable . ic_dialog_info )
. setMessage ( " Please install Log Collector application to collect the device log and send it to dev. " )
. setPositiveButton ( android . R . string . ok , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int whichButton ) {
Intent marketIntent = new Intent ( Intent . ACTION_VIEW , Uri . parse ( " market://search?q=pname: " + LOG_COLLECTOR_PACKAGE_NAME ) ) ;
marketIntent . addFlags ( Intent . FLAG_ACTIVITY_NEW_TASK ) ;
startActivity ( marketIntent ) ;
}
} )
. setNegativeButton ( android . R . string . cancel , null )
. show ( ) ;
}
else {
new AlertDialog . Builder ( this )
. setTitle ( getString ( R . string . app_name ) )
. setIcon ( android . R . drawable . ic_dialog_info )
2010-09-24 20:47:56 +01:00
. setMessage ( " Do you want to send a bug report to the dev? Please specify what problem is ocurring. \ n \ nMake sure you started & stopped the server before submitting " )
2010-09-16 02:39:31 +01:00
. setPositiveButton ( android . R . string . ok , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int whichButton ) {
intent . addFlags ( Intent . FLAG_ACTIVITY_NEW_TASK ) ;
intent . putExtra ( EXTRA_SEND_INTENT_ACTION , Intent . ACTION_SENDTO ) ;
final String email = " onaips@gmail.com " ;
intent . putExtra ( EXTRA_DATA , Uri . parse ( " mailto: " + email ) ) ;
intent . putExtra ( EXTRA_ADDITIONAL_INFO , " Problem Description: \ n \ n \ n \ n---------DEBUG-------- \ n " + getString ( R . string . device_info_fmt , getVersionNumber ( getApplicationContext ( ) ) , Build . MODEL , Build . VERSION . RELEASE , getFormattedKernelVersion ( ) , Build . DISPLAY ) ) ;
intent . putExtra ( Intent . EXTRA_SUBJECT , " droid VNC server: Debug Info " ) ;
intent . putExtra ( EXTRA_FORMAT , " time " ) ;
//The log can be filtered to contain data relevant only to your app
String [ ] filterSpecs = new String [ 4 ] ;
2011-10-29 11:56:13 +01:00
filterSpecs [ 0 ] = VNC_LOG + " :I " ;
filterSpecs [ 1 ] = VNC_LOG + " :D " ;
filterSpecs [ 2 ] = VNC_LOG + " :V " ;
2010-09-16 02:39:31 +01:00
filterSpecs [ 3 ] = " *:S " ;
intent . putExtra ( EXTRA_FILTER_SPECS , filterSpecs ) ;
startActivity ( intent ) ;
}
} )
. setNegativeButton ( android . R . string . cancel , null )
. show ( ) ;
}
}
private String getFormattedKernelVersion ( )
{
String procVersionStr ;
try {
BufferedReader reader = new BufferedReader ( new FileReader ( " /proc/version " ) , 256 ) ;
try {
procVersionStr = reader . readLine ( ) ;
} finally {
reader . close ( ) ;
}
final String PROC_VERSION_REGEX =
" \\ w+ \\ s+ " + /* ignore: Linux */
" \\ w+ \\ s+ " + /* ignore: version */
" ([^ \\ s]+) \\ s+ " + /* group 1: 2.6.22-omap1 */
" \\ (([^ \\ s@]+(?:@[^ \\ s.]+)?)[^)]* \\ ) \\ s+ " + /* group 2: (xxxxxx@xxxxx.constant) */
" \\ ([^)]+ \\ ) \\ s+ " + /* ignore: (gcc ..) */
" ([^ \\ s]+) \\ s+ " + /* group 3: #26 */
" (?:PREEMPT \\ s+)? " + /* ignore: PREEMPT (optional) */
" (.+) " ; /* group 4: date */
Pattern p = Pattern . compile ( PROC_VERSION_REGEX ) ;
Matcher m = p . matcher ( procVersionStr ) ;
if ( ! m . matches ( ) ) {
2011-10-29 11:56:13 +01:00
log ( " Regex did not match on /proc/version: " + procVersionStr ) ;
2010-09-16 02:39:31 +01:00
return " Unavailable " ;
} else if ( m . groupCount ( ) < 4 ) {
2011-10-29 11:56:13 +01:00
log ( " Regex match on /proc/version only returned " + m . groupCount ( )
2010-09-16 02:39:31 +01:00
+ " groups " ) ;
return " Unavailable " ;
} else {
return ( new StringBuilder ( m . group ( 1 ) ) . append ( " \ n " ) . append (
m . group ( 2 ) ) . append ( " " ) . append ( m . group ( 3 ) ) . append ( " \ n " )
. append ( m . group ( 4 ) ) ) . toString ( ) ;
}
} catch ( IOException e ) {
2011-10-29 11:56:13 +01:00
log ( " IO Exception when getting kernel version for Device Info screen " + e . getMessage ( ) ) ;
2010-09-16 02:39:31 +01:00
return " Unavailable " ;
}
}
private static String getVersionNumber ( Context context )
{
String version = " ? " ;
try
{
PackageInfo packagInfo = context . getPackageManager ( ) . getPackageInfo ( context . getPackageName ( ) , 0 ) ;
version = packagInfo . versionName ;
}
catch ( PackageManager . NameNotFoundException e ) { } ;
return version ;
}
2010-09-24 20:47:56 +01:00
static void writeCommand ( OutputStream os , String command ) throws Exception
{
os . write ( ( command + " \ n " ) . getBytes ( " ASCII " ) ) ;
}
2010-12-14 22:20:30 +00:00
public BroadcastReceiver mReceiver = new BroadcastReceiver ( ) {
2010-09-16 02:39:31 +01:00
@Override public void onReceive ( Context context , Intent intent ) {
NetworkInfo info = intent . getParcelableExtra ( ConnectivityManager . EXTRA_NETWORK_INFO ) ;
if ( info . getType ( ) = = ConnectivityManager . TYPE_MOBILE | | info . getType ( ) = = ConnectivityManager . TYPE_WIFI ) {
2011-10-29 11:56:13 +01:00
setStateLabels ( ServerManager . isServerRunning ( ) ) ;
2010-12-14 22:20:30 +00:00
2011-10-29 11:56:13 +01:00
}
2010-12-25 15:33:52 +00:00
}
2011-10-29 11:56:13 +01:00
} ;
2010-12-25 15:33:52 +00:00
2010-12-14 22:20:30 +00:00
2011-10-29 11:56:13 +01:00
static File findExecutableOnPath ( String executableName )
2010-12-14 22:20:30 +00:00
{
2011-10-29 11:56:13 +01:00
2010-12-14 22:20:30 +00:00
String systemPath = System . getenv ( " PATH " ) ;
String [ ] pathDirs = systemPath . split ( File . pathSeparator ) ;
File fullyQualifiedExecutable = null ;
for ( String pathDir : pathDirs )
{
File file = new File ( pathDir , executableName ) ;
if ( file . isFile ( ) )
{
fullyQualifiedExecutable = file ;
break ;
}
}
return fullyQualifiedExecutable ;
2011-10-29 11:56:13 +01:00
}
2010-09-24 20:47:56 +01:00
}