2016-11-18 16:49:51 +02:00
package im.status.ethereum ;
2016-03-23 16:48:26 +02:00
2017-08-07 14:21:07 +02:00
import android.content.Context ;
2018-03-14 12:49:41 +03:00
import android.annotation.TargetApi ;
import android.support.annotation.Nullable ;
2016-06-08 12:43:02 +02:00
import android.app.AlertDialog ;
2017-08-06 23:46:43 +02:00
import android.app.ActivityManager ;
2016-06-08 12:43:02 +02:00
import android.content.DialogInterface ;
import android.content.DialogInterface.OnCancelListener ;
2016-07-20 12:03:05 +03:00
import android.content.DialogInterface.OnClickListener ;
2018-02-17 13:05:03 +08:00
import android.content.pm.PackageManager ;
2018-03-01 15:02:21 +01:00
import android.net.Uri ;
import android.os.Build ;
2017-08-07 14:21:07 +02:00
import android.os.Looper ;
2018-02-17 13:05:03 +08:00
import android.support.v4.app.ActivityCompat ;
2017-08-07 14:21:07 +02:00
import android.util.Log ;
2016-06-16 07:23:11 +03:00
import android.content.Intent ;
2017-11-12 19:50:42 +02:00
import android.content.SharedPreferences ;
2016-06-21 12:03:39 +03:00
import android.content.res.Configuration ;
2018-03-01 15:02:21 +01:00
import android.provider.Settings ;
2016-07-20 12:03:05 +03:00
import android.os.Bundle ;
2017-11-12 19:50:42 +02:00
2016-07-20 12:03:05 +03:00
import com.facebook.react.ReactActivity ;
2018-03-14 12:49:41 +03:00
import com.facebook.react.modules.core.PermissionListener ;
2017-10-19 06:38:20 +02:00
import org.devio.rn.splashscreen.SplashScreen ;
2017-05-10 19:42:52 +03:00
import com.testfairy.TestFairy ;
2016-06-01 11:41:50 +03:00
2016-07-20 12:03:05 +03:00
import java.util.Properties ;
2016-07-18 19:29:51 +03:00
2018-02-17 13:05:03 +08:00
public class MainActivity extends ReactActivity
implements ActivityCompat . OnRequestPermissionsResultCallback {
private static final int PERMISSION_REQUEST_CAMERA = 0 ;
2017-08-06 23:46:43 +02:00
2018-03-14 12:49:41 +03:00
@Nullable private PermissionListener mPermissionListener ;
2017-08-07 14:21:07 +02:00
private static void registerUncaughtExceptionHandler ( final Context context ) {
2017-08-06 23:46:43 +02:00
final Thread . UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread . getDefaultUncaughtExceptionHandler ();
Thread . setDefaultUncaughtExceptionHandler ( new Thread . UncaughtExceptionHandler () {
@Override
public void uncaughtException ( final Thread thread , final Throwable t ) {
new Thread () {
@Override
public void run () {
Looper . prepare ();
2017-08-07 14:21:07 +02:00
new AlertDialog . Builder ( context )
2017-08-06 23:46:43 +02:00
. setTitle ( "Error" )
. setMessage ( t . toString ())
. setNegativeButton ( "Exit" , new DialogInterface . OnClickListener () {
public void onClick ( final DialogInterface dialog , final int id ) {
dialog . dismiss ();
defaultUncaughtExceptionHandler . uncaughtException ( thread , t );
}
}). show ();
Looper . loop ();
}
}. start ();
}
});
}
2017-08-07 14:21:07 +02:00
private ActivityManager getActivityManager () {
return ( ActivityManager ) this . getSystemService ( ACTIVITY_SERVICE );
}
private ActivityManager . MemoryInfo getAvailableMemory ( final ActivityManager activityManager ) {
final ActivityManager . MemoryInfo memoryInfo = new ActivityManager . MemoryInfo ();
2017-08-06 23:46:43 +02:00
activityManager . getMemoryInfo ( memoryInfo );
return memoryInfo ;
}
2016-09-07 20:35:04 +03:00
protected void configureStatus () {
2016-06-08 12:43:02 +02:00
// Required because of crazy APN settings redirecting localhost (found in GB)
2016-03-23 17:02:53 +02:00
Properties properties = System . getProperties ();
properties . setProperty ( "http.nonProxyHosts" , "localhost|127.0.0.1" );
properties . setProperty ( "https.nonProxyHosts" , "localhost|127.0.0.1" );
}
2018-03-01 15:02:21 +01:00
private Intent createNotificationSettingsIntent () {
final Intent intent = new Intent ();
if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . O ) {
intent . setAction ( Settings . ACTION_APP_NOTIFICATION_SETTINGS );
intent . putExtra ( "app_package" , getPackageName ());
intent . putExtra ( "app_uid" , getApplicationInfo (). uid );
} else {
intent . setAction ( Settings . ACTION_APPLICATION_DETAILS_SETTINGS );
intent . addCategory ( Intent . CATEGORY_DEFAULT );
intent . setData ( Uri . parse ( "package:" + getPackageName ()));
}
return intent ;
}
@Override
public void onNewIntent ( final Intent intent ) {
2018-03-27 06:20:29 +01:00
if ( intent . getDataString () != null && intent . getData (). getScheme (). startsWith ( "app-settings" )) {
startActivity ( createNotificationSettingsIntent ());
2018-03-01 15:02:21 +01:00
}
}
2016-06-08 12:43:02 +02:00
@Override
protected void onCreate ( Bundle savedInstanceState ) {
2018-03-01 15:02:21 +01:00
2017-08-07 14:21:07 +02:00
// Make sure we get an Alert for every uncaught exceptions
registerUncaughtExceptionHandler ( MainActivity . this );
// Report memory details for this application
final ActivityManager activityManager = getActivityManager ();
Log . v ( "RNBootstrap" , "Available system memory " + getAvailableMemory ( activityManager ). availMem + ", maximum usable application memory " + activityManager . getLargeMemoryClass () + "M" );
2017-08-06 23:46:43 +02:00
2016-11-15 13:43:48 +02:00
SplashScreen . show ( this );
2016-06-08 12:43:02 +02:00
super . onCreate ( savedInstanceState );
2017-08-09 18:30:08 +03:00
if ( BuildConfig . TESTFAIRY_ENABLED == "1" ) {
TestFairy . begin ( this , "969f6c921cb435cea1d41d1ea3f5b247d6026d55" );
}
2016-06-08 12:43:02 +02:00
2017-11-12 19:50:42 +02:00
if ( ! shouldShowRootedNotification ()) {
2016-09-07 20:35:04 +03:00
configureStatus ();
2016-06-08 12:43:02 +02:00
} else {
2016-07-20 12:03:05 +03:00
AlertDialog dialog = new AlertDialog . Builder ( MainActivity . this )
. setMessage ( getResources (). getString ( R . string . root_warning ))
2016-06-08 12:43:02 +02:00
. setPositiveButton ( getResources (). getString ( R . string . root_okay ), new OnClickListener () {
@Override
public void onClick ( DialogInterface dialog , int which ) {
2017-11-12 19:50:42 +02:00
rejectRootedNotification ();
2016-06-08 12:43:02 +02:00
dialog . dismiss ();
2016-09-07 20:35:04 +03:00
configureStatus ();
2016-06-08 12:43:02 +02:00
}
2016-07-20 12:03:05 +03:00
})
. setNegativeButton ( getResources (). getString ( R . string . root_cancel ), new OnClickListener () {
2016-06-08 12:43:02 +02:00
@Override
public void onClick ( DialogInterface dialog , int which ) {
dialog . dismiss ();
MainActivity . this . finishAffinity ();
}
2016-07-20 12:03:05 +03:00
})
. setOnCancelListener ( new OnCancelListener () {
2016-06-08 12:43:02 +02:00
@Override
public void onCancel ( DialogInterface dialog ) {
dialog . dismiss ();
MainActivity . this . finishAffinity ();
}
2016-07-20 12:03:05 +03:00
})
. create ();
2016-06-08 12:43:02 +02:00
dialog . show ();
}
2017-10-12 21:49:48 +02:00
Thread thread = new Thread () {
@Override
public void run () {
System . loadLibrary ( "status-logs" );
}
};
thread . start ();
2016-03-23 17:02:53 +02:00
}
2016-06-16 07:23:11 +03:00
@Override
protected void onDestroy () {
super . onDestroy ();
}
2016-03-23 16:48:26 +02:00
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName () {
2016-05-19 18:32:38 +02:00
return "StatusIm" ;
2016-03-23 16:48:26 +02:00
}
2016-06-21 12:03:39 +03:00
@Override
public void onConfigurationChanged ( Configuration newConfig ) {
super . onConfigurationChanged ( newConfig );
Intent intent = new Intent ( "onConfigurationChanged" );
intent . putExtra ( "newConfig" , newConfig );
this . sendBroadcast ( intent );
}
2017-11-12 19:50:42 +02:00
private static final String REJECTED_ROOTED_NOTIFICATION = "rejectedRootedNotification" ;
private static final Integer FREQUENCY_OF_REMINDER_IN_PERCENT = 5 ;
private boolean shouldShowRootedNotification () {
if ( RootUtil . isDeviceRooted ()) {
if ( userRejectedRootedNotification ()) {
return (( Math . random () * 100 ) < FREQUENCY_OF_REMINDER_IN_PERCENT );
} else return true ;
} else {
return false ;
}
}
private boolean userRejectedRootedNotification () {
SharedPreferences preferences = getPreferences ( 0 );
return preferences . getBoolean ( REJECTED_ROOTED_NOTIFICATION , false );
}
private void rejectRootedNotification () {
SharedPreferences preferences = getPreferences ( 0 );
SharedPreferences . Editor editor = preferences . edit ();
editor . putBoolean ( REJECTED_ROOTED_NOTIFICATION , true );
editor . commit ();
}
2018-02-17 13:05:03 +08:00
2018-03-14 12:49:41 +03:00
@TargetApi ( Build . VERSION_CODES . M )
public void requestPermissions ( String [] permissions , int requestCode , PermissionListener listener ) {
mPermissionListener = listener ;
requestPermissions ( permissions , requestCode );
}
2018-02-17 13:05:03 +08:00
@Override
public void onRequestPermissionsResult ( int requestCode , String [] permissions , int [] grantResults ) {
2018-03-14 12:49:41 +03:00
if ( mPermissionListener != null && mPermissionListener . onRequestPermissionsResult ( requestCode , permissions , grantResults )) {
mPermissionListener = null ;
}
2018-02-17 13:05:03 +08:00
if ( grantResults . length == 1 && grantResults [ 0 ] == PackageManager . PERMISSION_GRANTED ) {
// Permission has been granted. Start camera preview Activity.
com . github . alinz . reactnativewebviewbridge . WebViewBridgeManager . grantAccess ( requestCode );
}
}
2016-03-23 16:48:26 +02:00
}