restore login activity even if app was killed

This commit is contained in:
DrKLO 2014-04-05 03:00:38 +04:00
parent c783e25d69
commit e8b3375df0
6 changed files with 169 additions and 204 deletions

View file

@ -57,6 +57,7 @@ import java.io.InputStreamReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
public class LaunchActivity extends ActionBarActivity implements NotificationCenter.NotificationCenterDelegate, MessagesActivity.MessagesActivityDelegate { public class LaunchActivity extends ActionBarActivity implements NotificationCenter.NotificationCenterDelegate, MessagesActivity.MessagesActivityDelegate {
private boolean finished = false; private boolean finished = false;
@ -90,8 +91,15 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen
finish(); finish();
return; return;
} }
Intent intent2 = new Intent(this, IntroActivity.class); SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo", MODE_PRIVATE);
startActivity(intent2); Map<String, ?> 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(); finish();
return; return;
} }

View file

@ -12,6 +12,7 @@ import android.animation.Animator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.graphics.Point; import android.graphics.Point;
import android.os.Bundle; import android.os.Bundle;
@ -24,11 +25,15 @@ import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import org.telegram.messenger.FileLog;
import org.telegram.messenger.LocaleController; import org.telegram.messenger.LocaleController;
import org.telegram.messenger.R; import org.telegram.messenger.R;
import org.telegram.messenger.Utilities; import org.telegram.messenger.Utilities;
import org.telegram.ui.Views.SlideView; import org.telegram.ui.Views.SlideView;
import java.util.Map;
import java.util.Set;
public class LoginActivity extends ActionBarActivity implements SlideView.SlideViewDelegate { public class LoginActivity extends ActionBarActivity implements SlideView.SlideViewDelegate {
private int currentViewNum = 0; private int currentViewNum = 0;
private SlideView[] views = new SlideView[3]; private SlideView[] views = new SlideView[3];
@ -57,6 +62,90 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV
ApplicationLoader.lastPauseTime = System.currentTimeMillis(); 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<String, ?> params = preferences.getAll();
for (Map.Entry<String, ?> 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<String> 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) { public void ShowAlertDialog(final Activity activity, final String message) {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
@ -95,12 +184,16 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV
getSupportActionBar().setTitle(views[0].getHeaderName()); getSupportActionBar().setTitle(views[0].getHeaderName());
savedInstanceState = loadCurrentState();
if (savedInstanceState != null) { if (savedInstanceState != null) {
currentViewNum = savedInstanceState.getInt("currentViewNum", 0); currentViewNum = savedInstanceState.getInt("currentViewNum", 0);
} }
for (int a = 0; a < views.length; a++) { for (int a = 0; a < views.length; a++) {
SlideView v = views[a]; SlideView v = views[a];
if (v != null) { if (v != null) {
if (savedInstanceState != null) {
v.restoreStateParams(savedInstanceState);
}
v.delegate = this; v.delegate = this;
v.setVisibility(currentViewNum == a ? View.VISIBLE : View.GONE); v.setVisibility(currentViewNum == a ? View.VISIBLE : View.GONE);
} }
@ -239,7 +332,7 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putInt("currentViewNum", currentViewNum); saveCurrentState();
} }
@Override @Override
@ -247,5 +340,6 @@ public class LoginActivity extends ActionBarActivity implements SlideView.SlideV
Intent intent2 = new Intent(this, LaunchActivity.class); Intent intent2 = new Intent(this, LaunchActivity.class);
startActivity(intent2); startActivity(intent2);
finish(); finish();
clearCurrentState();
} }
} }

View file

@ -11,8 +11,6 @@ package org.telegram.ui;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.Editable; import android.text.Editable;
@ -400,55 +398,26 @@ public class LoginActivityPhoneView extends SlideView implements AdapterView.OnI
} }
@Override @Override
protected Parcelable onSaveInstanceState() { public void saveStateParams(Bundle bundle) {
Parcelable superState = super.onSaveInstanceState(); String code = codeField.getText().toString();
return new SavedState(superState, phoneField.getText().toString(), 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 @Override
protected void onRestoreInstanceState(Parcelable state) { public void restoreStateParams(Bundle bundle) {
SavedState savedState = (SavedState) state; String code = bundle.getString("phoneview_code");
super.onRestoreInstanceState(savedState.getSuperState()); if (code != null) {
codeField.setText(savedState.code); codeField.setText(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 = "";
}
} }
String phone = bundle.getString("phoneview_phone");
private SavedState(Parcel in) { if (phone != null) {
super(in); phoneField.setText(phone);
phone = in.readString();
code = in.readString();
} }
@Override
public void writeToParcel(Parcel destination, int flags) {
super.writeToParcel(destination, flags);
destination.writeString(phone);
destination.writeString(code);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
} }
} }

View file

@ -10,8 +10,6 @@ package org.telegram.ui;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
@ -40,10 +38,6 @@ public class LoginActivityRegisterView extends SlideView {
private String requestPhone; private String requestPhone;
private String phoneHash; private String phoneHash;
private String phoneCode; private String phoneCode;
//private BackupImageView avatarImage;
//public AvatarUpdater avatarUpdater = new AvatarUpdater();
//private TLRPC.PhotoSize avatarPhoto = null;
//private TLRPC.PhotoSize avatarPhotoBig = null;
private Bundle currentParams; private Bundle currentParams;
public LoginActivityRegisterView(Context context) { public LoginActivityRegisterView(Context context) {
@ -62,25 +56,10 @@ public class LoginActivityRegisterView extends SlideView {
protected void onFinishInflate() { protected void onFinishInflate() {
super.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 = (EditText)findViewById(R.id.login_first_name_field);
firstNameField.setHint(LocaleController.getString("FirstName", R.string.FirstName)); firstNameField.setHint(LocaleController.getString("FirstName", R.string.FirstName));
lastNameField = (EditText)findViewById(R.id.login_last_name_field); lastNameField = (EditText)findViewById(R.id.login_last_name_field);
lastNameField.setHint(LocaleController.getString("LastName", R.string.LastName)); 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 textView = (TextView)findViewById(R.id.login_register_info);
textView.setText(LocaleController.getString("RegisterText", R.string.RegisterText)); 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 @Override
@ -184,7 +118,6 @@ public class LoginActivityRegisterView extends SlideView {
phoneHash = params.getString("phoneHash"); phoneHash = params.getString("phoneHash");
phoneCode = params.getString("code"); phoneCode = params.getString("code");
currentParams = params; currentParams = params;
resetAvatar();
} }
@Override @Override
@ -249,63 +182,33 @@ public class LoginActivityRegisterView extends SlideView {
} }
@Override @Override
protected Parcelable onSaveInstanceState() { public void saveStateParams(Bundle bundle) {
Parcelable superState = super.onSaveInstanceState(); String first = firstNameField.getText().toString();
return new SavedState(superState, firstNameField.getText().toString(), lastNameField.getText().toString(), currentParams); 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 @Override
protected void onRestoreInstanceState(Parcelable state) { public void restoreStateParams(Bundle bundle) {
SavedState savedState = (SavedState) state; currentParams = bundle.getBundle("registerview_params");
super.onRestoreInstanceState(savedState.getSuperState());
currentParams = savedState.params;
if (currentParams != null) { if (currentParams != null) {
setParams(currentParams); setParams(currentParams);
} }
firstNameField.setText(savedState.firstName); String first = bundle.getString("registerview_first");
lastNameField.setText(savedState.lastName); if (first != null) {
} firstNameField.setText(first);
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 last = bundle.getString("registerview_last");
private SavedState(Parcel in) { if (last != null) {
super(in); lastNameField.setText(last);
firstName = in.readString();
lastName = in.readString();
params = in.readBundle();
} }
@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<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
} }
} }

View file

@ -10,8 +10,6 @@ package org.telegram.ui;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Html; import android.text.Html;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -51,7 +49,7 @@ public class LoginActivitySmsView extends SlideView implements NotificationCente
private Timer timeTimer; private Timer timeTimer;
private final Integer timerSync = 1; private final Integer timerSync = 1;
private int time = 60000; private volatile int time = 60000;
private double lastCurrentTime; private double lastCurrentTime;
private boolean waitingForSms = false; private boolean waitingForSms = false;
@ -380,47 +378,32 @@ public class LoginActivitySmsView extends SlideView implements NotificationCente
} }
@Override @Override
protected Parcelable onSaveInstanceState() { public void saveStateParams(Bundle bundle) {
Parcelable superState = super.onSaveInstanceState(); String code = codeField.getText().toString();
return new SavedState(superState, currentParams); 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 @Override
protected void onRestoreInstanceState(Parcelable state) { public void restoreStateParams(Bundle bundle) {
SavedState savedState = (SavedState) state; currentParams = bundle.getBundle("smsview_params");
super.onRestoreInstanceState(savedState.getSuperState());
currentParams = savedState.params;
if (currentParams != null) { if (currentParams != null) {
setParams(currentParams); setParams(currentParams);
} }
} String code = bundle.getString("smsview_code");
if (code != null) {
protected static class SavedState extends BaseSavedState { codeField.setText(code);
public Bundle params;
private SavedState(Parcelable superState, Bundle p1) {
super(superState);
params = p1;
} }
Integer t = bundle.getInt("time");
private SavedState(Parcel in) { if (t != 0) {
super(in); time = t;
params = in.readBundle();
} }
@Override
public void writeToParcel(Parcel destination, int flags) {
super.writeToParcel(destination, flags);
destination.writeBundle(params);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
} }
} }

View file

@ -60,4 +60,12 @@ public class SlideView extends LinearLayout {
public void onDestroyActivity() { public void onDestroyActivity() {
delegate = null; delegate = null;
} }
public void saveStateParams(Bundle bundle) {
}
public void restoreStateParams(Bundle bundle) {
}
} }