From e8b3375df04813eaac6f2bb6e512def163ccba10 Mon Sep 17 00:00:00 2001 From: DrKLO Date: Sat, 5 Apr 2014 03:00:38 +0400 Subject: [PATCH] restore login activity even if app was killed --- .../java/org/telegram/ui/LaunchActivity.java | 12 +- .../java/org/telegram/ui/LoginActivity.java | 96 +++++++++++- .../telegram/ui/LoginActivityPhoneView.java | 63 ++------ .../ui/LoginActivityRegisterView.java | 137 +++--------------- .../org/telegram/ui/LoginActivitySmsView.java | 57 +++----- .../java/org/telegram/ui/Views/SlideView.java | 8 + 6 files changed, 169 insertions(+), 204 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index 0847666fb..0bc6f2fad 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -57,6 +57,7 @@ import java.io.InputStreamReader; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Map; public class LaunchActivity extends ActionBarActivity implements NotificationCenter.NotificationCenterDelegate, MessagesActivity.MessagesActivityDelegate { private boolean finished = false; @@ -90,8 +91,15 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen finish(); return; } - Intent intent2 = new Intent(this, IntroActivity.class); - startActivity(intent2); + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo", MODE_PRIVATE); + Map state = preferences.getAll(); + if (state.isEmpty()) { + Intent intent2 = new Intent(this, IntroActivity.class); + startActivity(intent2); + } else { + Intent intent2 = new Intent(this, LoginActivity.class); + startActivity(intent2); + } finish(); return; } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivity.java index bfad47660..60ebbbbef 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivity.java @@ -12,6 +12,7 @@ import android.animation.Animator; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.PixelFormat; import android.graphics.Point; import android.os.Bundle; @@ -24,11 +25,15 @@ import android.view.animation.AccelerateDecelerateInterpolator; import android.widget.ImageView; import android.widget.TextView; +import org.telegram.messenger.FileLog; import org.telegram.messenger.LocaleController; import org.telegram.messenger.R; import org.telegram.messenger.Utilities; import org.telegram.ui.Views.SlideView; +import java.util.Map; +import java.util.Set; + public class LoginActivity extends ActionBarActivity implements SlideView.SlideViewDelegate { private int currentViewNum = 0; private SlideView[] views = new SlideView[3]; @@ -57,6 +62,90 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV ApplicationLoader.lastPauseTime = System.currentTimeMillis(); } + private void saveCurrentState() { + try { + Bundle bundle = new Bundle(); + bundle.putInt("currentViewNum", currentViewNum); + for (int a = 0; a <= currentViewNum; a++) { + SlideView v = views[a]; + if (v != null) { + v.saveStateParams(bundle); + } + } + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo", MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.clear(); + putBundleToEditor(bundle, editor, null); + editor.commit(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + } + + private Bundle loadCurrentState() { + try { + Bundle bundle = new Bundle(); + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo", MODE_PRIVATE); + Map params = preferences.getAll(); + for (Map.Entry entry : params.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + String[] args = key.split("_\\|_"); + if (args.length == 1) { + if (value instanceof String) { + bundle.putString(key, (String) value); + } else if (value instanceof Integer) { + bundle.putInt(key, (Integer) value); + } + } else if (args.length == 2) { + Bundle inner = bundle.getBundle(args[0]); + if (inner == null) { + inner = new Bundle(); + bundle.putBundle(args[0], inner); + } + if (value instanceof String) { + inner.putString(args[1], (String) value); + } else if (value instanceof Integer) { + inner.putInt(args[1], (Integer) value); + } + } + } + return bundle; + } catch (Exception e) { + FileLog.e("tmessages", e); + } + return null; + } + + private void clearCurrentState() { + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo", MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.clear(); + editor.commit(); + } + + private void putBundleToEditor(Bundle bundle, SharedPreferences.Editor editor, String prefix) { + Set keys = bundle.keySet(); + for (String key : keys) { + Object obj = bundle.get(key); + if (obj instanceof String) { + if (prefix != null) { + editor.putString(prefix + "_|_" + key, (String) obj); + } else { + editor.putString(key, (String) obj); + } + } else if (obj instanceof Integer) { + if (prefix != null) { + editor.putInt(prefix + "_|_" + key, (Integer) obj); + } else { + editor.putInt(key, (Integer) obj); + } + } else if (obj instanceof Bundle) { + putBundleToEditor((Bundle)obj, editor, key); + } + } + } + public void ShowAlertDialog(final Activity activity, final String message) { activity.runOnUiThread(new Runnable() { @Override @@ -95,12 +184,16 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV getSupportActionBar().setTitle(views[0].getHeaderName()); + savedInstanceState = loadCurrentState(); if (savedInstanceState != null) { currentViewNum = savedInstanceState.getInt("currentViewNum", 0); } for (int a = 0; a < views.length; a++) { SlideView v = views[a]; if (v != null) { + if (savedInstanceState != null) { + v.restoreStateParams(savedInstanceState); + } v.delegate = this; v.setVisibility(currentViewNum == a ? View.VISIBLE : View.GONE); } @@ -239,7 +332,7 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - outState.putInt("currentViewNum", currentViewNum); + saveCurrentState(); } @Override @@ -247,5 +340,6 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV Intent intent2 = new Intent(this, LaunchActivity.class); startActivity(intent2); finish(); + clearCurrentState(); } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java index f01445c2b..e6f7bda6b 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java @@ -11,8 +11,6 @@ package org.telegram.ui; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.os.Parcel; -import android.os.Parcelable; import android.support.v7.app.ActionBarActivity; import android.telephony.TelephonyManager; import android.text.Editable; @@ -400,55 +398,26 @@ public class LoginActivityPhoneView extends SlideView implements AdapterView.OnI } @Override - protected Parcelable onSaveInstanceState() { - Parcelable superState = super.onSaveInstanceState(); - return new SavedState(superState, phoneField.getText().toString(), codeField.getText().toString()); + public void saveStateParams(Bundle bundle) { + String code = codeField.getText().toString(); + if (code != null && code.length() != 0) { + bundle.putString("phoneview_code", code); + } + String phone = phoneField.getText().toString(); + if (phone != null && phone.length() != 0) { + bundle.putString("phoneview_phone", phone); + } } @Override - protected void onRestoreInstanceState(Parcelable state) { - SavedState savedState = (SavedState) state; - super.onRestoreInstanceState(savedState.getSuperState()); - codeField.setText(savedState.code); - phoneField.setText(savedState.phone); - } - - protected static class SavedState extends BaseSavedState { - public String phone; - public String code; - - private SavedState(Parcelable superState, String text1, String text2) { - super(superState); - phone = text1; - code = text2; - if (phone == null) { - phone = ""; - } - if (code == null) { - code = ""; - } + public void restoreStateParams(Bundle bundle) { + String code = bundle.getString("phoneview_code"); + if (code != null) { + codeField.setText(code); } - - private SavedState(Parcel in) { - super(in); - phone = in.readString(); - code = in.readString(); + String phone = bundle.getString("phoneview_phone"); + if (phone != null) { + phoneField.setText(phone); } - - @Override - public void writeToParcel(Parcel destination, int flags) { - super.writeToParcel(destination, flags); - destination.writeString(phone); - destination.writeString(code); - } - - public static final Parcelable.Creator CREATOR = new Creator() { - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityRegisterView.java b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityRegisterView.java index 572320f3f..2a128493e 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityRegisterView.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityRegisterView.java @@ -10,8 +10,6 @@ package org.telegram.ui; import android.content.Context; import android.os.Bundle; -import android.os.Parcel; -import android.os.Parcelable; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.View; @@ -40,10 +38,6 @@ public class LoginActivityRegisterView extends SlideView { private String requestPhone; private String phoneHash; private String phoneCode; - //private BackupImageView avatarImage; - //public AvatarUpdater avatarUpdater = new AvatarUpdater(); - //private TLRPC.PhotoSize avatarPhoto = null; - //private TLRPC.PhotoSize avatarPhotoBig = null; private Bundle currentParams; public LoginActivityRegisterView(Context context) { @@ -62,25 +56,10 @@ public class LoginActivityRegisterView extends SlideView { protected void onFinishInflate() { super.onFinishInflate(); -// avatarUpdater.parentActivity = (Activity)delegate; -// avatarUpdater.delegate = new AvatarUpdater.AvatarUpdaterDelegate() { -// @Override -// public void didUploadedPhoto(TLRPC.InputFile file, TLRPC.PhotoSize small, TLRPC.PhotoSize big) { -// avatarPhotoBig = big; -// avatarPhoto = small; -// if (avatarImage != null) { -// avatarImage.setImage(small.location, null, R.drawable.user_placeholder); -// } -// } -// }; -// avatarUpdater.returnOnly = true; - - //ImageButton avatarButton = (ImageButton)findViewById(R.id.settings_change_avatar_button); firstNameField = (EditText)findViewById(R.id.login_first_name_field); firstNameField.setHint(LocaleController.getString("FirstName", R.string.FirstName)); lastNameField = (EditText)findViewById(R.id.login_last_name_field); lastNameField.setHint(LocaleController.getString("LastName", R.string.LastName)); - //avatarImage = (BackupImageView)findViewById(R.id.settings_avatar_image); TextView textView = (TextView)findViewById(R.id.login_register_info); textView.setText(LocaleController.getString("RegisterText", R.string.RegisterText)); @@ -107,51 +86,6 @@ public class LoginActivityRegisterView extends SlideView { } }); -// avatarButton.setOnClickListener(new View.OnClickListener() { -// @Override -// public void onClick(View view) { -// AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); -// -// CharSequence[] items; -// -// if (avatarPhoto != null) { -// items = new CharSequence[]{getString(R.string.FromCamera), getString(R.string.FromGalley), getString(R.string.DeletePhoto)}; -// } else { -// items = new CharSequence[]{getString(R.string.FromCamera), getString(R.string.FromGalley)}; -// } -// -// builder.setItems(items, new DialogInterface.OnClickListener() { -// @Override -// public void onClick(DialogInterface dialogInterface, int i) { -// if (i == 0) { -// avatarUpdater.openCamera(); -// } else if (i == 1) { -// avatarUpdater.openGallery(); -// } else if (i == 2) { -// resetAvatar(); -// } -// } -// }); -// builder.show().setCanceledOnTouchOutside(true); -// } -// }); - } - - public void resetAvatar() { -// avatarPhoto = null; -// avatarPhotoBig = null; -// if (avatarImage != null) { -// avatarImage.setImageResource(R.drawable.user_placeholder); -// } - } - - @Override - public void onDestroyActivity() { - super.onDestroyActivity(); -// if (avatarUpdater != null) { -// avatarUpdater.clear(); -// avatarUpdater = null; -// } } @Override @@ -184,7 +118,6 @@ public class LoginActivityRegisterView extends SlideView { phoneHash = params.getString("phoneHash"); phoneCode = params.getString("code"); currentParams = params; - resetAvatar(); } @Override @@ -249,63 +182,33 @@ public class LoginActivityRegisterView extends SlideView { } @Override - protected Parcelable onSaveInstanceState() { - Parcelable superState = super.onSaveInstanceState(); - return new SavedState(superState, firstNameField.getText().toString(), lastNameField.getText().toString(), currentParams); + public void saveStateParams(Bundle bundle) { + String first = firstNameField.getText().toString(); + if (first != null && first.length() != 0) { + bundle.putString("registerview_first", first); + } + String last = lastNameField.getText().toString(); + if (last != null && last.length() != 0) { + bundle.putString("registerview_last", last); + } + if (currentParams != null) { + bundle.putBundle("registerview_params", currentParams); + } } @Override - protected void onRestoreInstanceState(Parcelable state) { - SavedState savedState = (SavedState) state; - super.onRestoreInstanceState(savedState.getSuperState()); - currentParams = savedState.params; + public void restoreStateParams(Bundle bundle) { + currentParams = bundle.getBundle("registerview_params"); if (currentParams != null) { setParams(currentParams); } - firstNameField.setText(savedState.firstName); - lastNameField.setText(savedState.lastName); - } - - protected static class SavedState extends BaseSavedState { - public String firstName; - public String lastName; - public Bundle params; - - private SavedState(Parcelable superState, String text1, String text2, Bundle p1) { - super(superState); - firstName = text1; - lastName = text2; - if (firstName == null) { - firstName = ""; - } - if (lastName == null) { - lastName = ""; - } - params = p1; + String first = bundle.getString("registerview_first"); + if (first != null) { + firstNameField.setText(first); } - - private SavedState(Parcel in) { - super(in); - firstName = in.readString(); - lastName = in.readString(); - params = in.readBundle(); + String last = bundle.getString("registerview_last"); + if (last != null) { + lastNameField.setText(last); } - - @Override - public void writeToParcel(Parcel destination, int flags) { - super.writeToParcel(destination, flags); - destination.writeString(firstName); - destination.writeString(lastName); - destination.writeBundle(params); - } - - public static final Parcelable.Creator CREATOR = new Creator() { - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivitySmsView.java b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivitySmsView.java index fabb2fd36..7bd39f869 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivitySmsView.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivitySmsView.java @@ -10,8 +10,6 @@ package org.telegram.ui; import android.content.Context; import android.os.Bundle; -import android.os.Parcel; -import android.os.Parcelable; import android.text.Html; import android.util.AttributeSet; import android.view.KeyEvent; @@ -51,7 +49,7 @@ public class LoginActivitySmsView extends SlideView implements NotificationCente private Timer timeTimer; private final Integer timerSync = 1; - private int time = 60000; + private volatile int time = 60000; private double lastCurrentTime; private boolean waitingForSms = false; @@ -380,47 +378,32 @@ public class LoginActivitySmsView extends SlideView implements NotificationCente } @Override - protected Parcelable onSaveInstanceState() { - Parcelable superState = super.onSaveInstanceState(); - return new SavedState(superState, currentParams); + public void saveStateParams(Bundle bundle) { + String code = codeField.getText().toString(); + if (code != null && code.length() != 0) { + bundle.putString("smsview_code", code); + } + if (currentParams != null) { + bundle.putBundle("smsview_params", currentParams); + } + if (time != 0) { + bundle.putInt("time", time); + } } @Override - protected void onRestoreInstanceState(Parcelable state) { - SavedState savedState = (SavedState) state; - super.onRestoreInstanceState(savedState.getSuperState()); - currentParams = savedState.params; + public void restoreStateParams(Bundle bundle) { + currentParams = bundle.getBundle("smsview_params"); if (currentParams != null) { setParams(currentParams); } - } - - protected static class SavedState extends BaseSavedState { - public Bundle params; - - private SavedState(Parcelable superState, Bundle p1) { - super(superState); - params = p1; + String code = bundle.getString("smsview_code"); + if (code != null) { + codeField.setText(code); } - - private SavedState(Parcel in) { - super(in); - params = in.readBundle(); + Integer t = bundle.getInt("time"); + if (t != 0) { + time = t; } - - @Override - public void writeToParcel(Parcel destination, int flags) { - super.writeToParcel(destination, flags); - destination.writeBundle(params); - } - - public static final Parcelable.Creator CREATOR = new Creator() { - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Views/SlideView.java b/TMessagesProj/src/main/java/org/telegram/ui/Views/SlideView.java index 2f0d44866..3462651ed 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Views/SlideView.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Views/SlideView.java @@ -60,4 +60,12 @@ public class SlideView extends LinearLayout { public void onDestroyActivity() { delegate = null; } + + public void saveStateParams(Bundle bundle) { + + } + + public void restoreStateParams(Bundle bundle) { + + } }