1818import android .view .View ;
1919import android .widget .EditText ;
2020import android .widget .TextView ;
21+ import android .widget .Toast ;
2122
2223import com .chaquo .python .Python ;
2324import com .chaquo .python .android .AndroidPlatform ;
2930import com .moithepro .instatoolsandroid .jInstaloader .JInvalidArgumentException ;
3031import com .moithepro .instatoolsandroid .jInstaloader .JTwoFactorAuthRequiredException ;
3132
33+ import java .io .File ;
3234import java .io .FileNotFoundException ;
3335
3436public class LoginActivity extends AppCompatActivity {
@@ -76,8 +78,12 @@ protected void onCreate(Bundle savedInstanceState) {
7678 String u = username .getText ().toString ().trim ();
7779 String p = password .getText ().toString ().trim ();
7880 loader .login (u , p );
81+ File file = new File (this .getFilesDir (), "session" );
7982 try {
83+
84+ //loader.saveSession(file.getCanonicalPath());
8085 loader .saveSession ("session" );
86+
8187 } catch (Exception e ) {
8288 e .printStackTrace ();
8389 }
@@ -337,21 +343,24 @@ protected void onCreate(Bundle savedInstanceState) {
337343
338344 Handler mainHandler = new Handler (Looper .getMainLooper ());
339345 loginThread = new Thread (() -> {
340-
346+ File file = new File ( this . getFilesDir (), "session" );
341347 try {
342- loader .loadSession ("session" );
348+
349+ //loader.loadSession(file.getCanonicalPath());
350+ loader .loadSession (spu ,"session" );
343351 mainHandler .post (() -> {
344352 Intent intent = new Intent (this , UsersActivity .class );
345353 startActivity (intent );
346354 loggingIn = false ;
347355 password .setEnabled (true );
348356 username .setEnabled (true );
357+ Toast .makeText (this , "Logged in as @" + loader .getLoggedInUsername (), Toast .LENGTH_SHORT ).show ();
349358 login .setText (R .string .log_in );
350359 finish ();
351360 //setContentView(R.layout.users_activity);
352361
353362 });
354- }catch (FileNotFoundException e ) {
363+ } catch (FileNotFoundException er ) {
355364 mainHandler .post (() -> {
356365 AD = new AlertDialog .Builder (this )
357366 .setTitle ("Error" )
@@ -363,116 +372,14 @@ protected void onCreate(Bundle savedInstanceState) {
363372
364373 // A null listener allows the button to dismiss the dialog and take no further action.
365374 .show ();
366- loggingIn = false ;
367- password .setEnabled (true );
368- username .setEnabled (true );
369- login .setText (R .string .log_in );
375+
370376 });
371- }
372- catch (Exception e ){
377+ login ( spu , spp );
378+ } catch (Exception e ) {
373379 e .printStackTrace ();
380+ login (spu , spp );
374381 }
375- try {
376- loader .login (spu , spp );
377-
378- mainHandler .post (() -> {
379- Intent intent = new Intent (this , UsersActivity .class );
380- startActivity (intent );
381- loggingIn = false ;
382- password .setEnabled (true );
383- username .setEnabled (true );
384- login .setText (R .string .log_in );
385- finish ();
386- //setContentView(R.layout.users_activity);
387-
388- });
389- } catch (JInvalidArgumentException e ) {
390- mainHandler .post (() -> {
391- AD = new AlertDialog .Builder (this )
392- .setTitle ("Error" )
393- .setMessage ("Username Does not Exist" )
394-
395- // Specifying a listener allows you to take an action before dismissing the dialog.
396- // The dialog is automatically dismissed when a dialog button is clicked.
397- .setPositiveButton (android .R .string .ok , null )
398-
399- // A null listener allows the button to dismiss the dialog and take no further action.
400- .show ();
401- loggingIn = false ;
402- password .setEnabled (true );
403- username .setEnabled (true );
404- login .setText (R .string .log_in );
405- });
406- } catch (JConnectionException e ) {
407- mainHandler .post (() -> {
408- AD = new AlertDialog .Builder (this )
409- .setTitle ("Error" )
410- .setMessage ("Connection Failed" )
411-
412- // Specifying a listener allows you to take an action before dismissing the dialog.
413- // The dialog is automatically dismissed when a dialog button is clicked.
414- .setPositiveButton (android .R .string .ok , null )
415-
416- // A null listener allows the button to dismiss the dialog and take no further action.
417- .show ();
418- loggingIn = false ;
419- password .setEnabled (true );
420- username .setEnabled (true );
421- login .setText (R .string .log_in );
422- });
423382
424- } catch (JBadCredentialsException e ) {
425- mainHandler .post (() -> {
426- AD = new AlertDialog .Builder (this )
427- .setTitle ("Error" )
428- .setMessage ("Password is Wrong" )
429-
430- // Specifying a listener allows you to take an action before dismissing the dialog.
431- // The dialog is automatically dismissed when a dialog button is clicked.
432- .setPositiveButton (android .R .string .ok , null )
433-
434- // A null listener allows the button to dismiss the dialog and take no further action.
435- .show ();
436- loggingIn = false ;
437- password .setEnabled (true );
438- username .setEnabled (true );
439- login .setText (R .string .log_in );
440- });
441- } catch (JTwoFactorAuthRequiredException e ) {
442- mainHandler .post (() -> {
443- AD = new AlertDialog .Builder (this )
444- .setTitle ("Error" )
445- .setMessage ("Two Factor Authentication is required but is not supported right now. you can try logging in with another account." )
446-
447- // Specifying a listener allows you to take an action before dismissing the dialog.
448- // The dialog is automatically dismissed when a dialog button is clicked.
449- .setPositiveButton (android .R .string .ok , null )
450-
451- // A null listener allows the button to dismiss the dialog and take no further action.
452- .show ();
453- loggingIn = false ;
454- password .setEnabled (true );
455- username .setEnabled (true );
456- login .setText (R .string .log_in );
457- });
458- } catch (JException e ) {
459- mainHandler .post (() -> {
460- AD = new AlertDialog .Builder (this )
461- .setTitle ("Error" )
462- .setMessage ("Unknown Error" )
463-
464- // Specifying a listener allows you to take an action before dismissing the dialog.
465- // The dialog is automatically dismissed when a dialog button is clicked.
466- .setPositiveButton (android .R .string .ok , null )
467-
468- // A null listener allows the button to dismiss the dialog and take no further action.
469- .show ();
470- loggingIn = false ;
471- password .setEnabled (true );
472- username .setEnabled (true );
473- login .setText (R .string .log_in );
474- });
475- }
476383 });
477384 loginThread .start ();
478385 }
@@ -498,7 +405,118 @@ protected void onCreate(Bundle savedInstanceState) {
498405 });
499406
500407 }
408+ private void login (String u , String p ) {
409+ Handler mainHandler = new Handler (Looper .getMainLooper ());
410+ try {
411+ loader .login (u , p );
412+
413+ try {
414+ //loader.saveSession(file.getCanonicalPath());
415+ loader .saveSession ("session" );
416+ } catch (Exception e ) {
417+ e .printStackTrace ();
418+ }
501419
420+
421+ mainHandler .post (() -> {
422+ Intent intent = new Intent (this , UsersActivity .class );
423+ startActivity (intent );
424+ loggingIn = false ;
425+ password .setEnabled (true );
426+ username .setEnabled (true );
427+ login .setText (R .string .log_in );
428+ finish ();
429+ //setContentView(R.layout.users_activity);
430+
431+ });
432+ } catch (JInvalidArgumentException e ) {
433+ mainHandler .post (() -> {
434+ AD = new AlertDialog .Builder (this )
435+ .setTitle ("Error" )
436+ .setMessage ("Username Does not Exist" )
437+
438+ // Specifying a listener allows you to take an action before dismissing the dialog.
439+ // The dialog is automatically dismissed when a dialog button is clicked.
440+ .setPositiveButton (android .R .string .ok , null )
441+
442+ // A null listener allows the button to dismiss the dialog and take no further action.
443+ .show ();
444+ loggingIn = false ;
445+ password .setEnabled (true );
446+ username .setEnabled (true );
447+ login .setText (R .string .log_in );
448+ });
449+ } catch (JConnectionException e ) {
450+ mainHandler .post (() -> {
451+ AD = new AlertDialog .Builder (this )
452+ .setTitle ("Error" )
453+ .setMessage ("Connection Failed" )
454+
455+ // Specifying a listener allows you to take an action before dismissing the dialog.
456+ // The dialog is automatically dismissed when a dialog button is clicked.
457+ .setPositiveButton (android .R .string .ok , null )
458+
459+ // A null listener allows the button to dismiss the dialog and take no further action.
460+ .show ();
461+ loggingIn = false ;
462+ password .setEnabled (true );
463+ username .setEnabled (true );
464+ login .setText (R .string .log_in );
465+ });
466+
467+ } catch (JBadCredentialsException e ) {
468+ mainHandler .post (() -> {
469+ AD = new AlertDialog .Builder (this )
470+ .setTitle ("Error" )
471+ .setMessage ("Password is Wrong" )
472+
473+ // Specifying a listener allows you to take an action before dismissing the dialog.
474+ // The dialog is automatically dismissed when a dialog button is clicked.
475+ .setPositiveButton (android .R .string .ok , null )
476+
477+ // A null listener allows the button to dismiss the dialog and take no further action.
478+ .show ();
479+ loggingIn = false ;
480+ password .setEnabled (true );
481+ username .setEnabled (true );
482+ login .setText (R .string .log_in );
483+ });
484+ } catch (JTwoFactorAuthRequiredException e ) {
485+ mainHandler .post (() -> {
486+ AD = new AlertDialog .Builder (this )
487+ .setTitle ("Error" )
488+ .setMessage ("Two Factor Authentication is required but is not supported right now. you can try logging in with another account." )
489+
490+ // Specifying a listener allows you to take an action before dismissing the dialog.
491+ // The dialog is automatically dismissed when a dialog button is clicked.
492+ .setPositiveButton (android .R .string .ok , null )
493+
494+ // A null listener allows the button to dismiss the dialog and take no further action.
495+ .show ();
496+ loggingIn = false ;
497+ password .setEnabled (true );
498+ username .setEnabled (true );
499+ login .setText (R .string .log_in );
500+ });
501+ } catch (JException e ) {
502+ mainHandler .post (() -> {
503+ AD = new AlertDialog .Builder (this )
504+ .setTitle ("Error" )
505+ .setMessage ("Unknown Error" )
506+
507+ // Specifying a listener allows you to take an action before dismissing the dialog.
508+ // The dialog is automatically dismissed when a dialog button is clicked.
509+ .setPositiveButton (android .R .string .ok , null )
510+
511+ // A null listener allows the button to dismiss the dialog and take no further action.
512+ .show ();
513+ loggingIn = false ;
514+ password .setEnabled (true );
515+ username .setEnabled (true );
516+ login .setText (R .string .log_in );
517+ });
518+ }
519+ }
502520 @ Override
503521 protected void onDestroy () {
504522 super .onDestroy ();
0 commit comments