From 22918f143a5d54cc56282575246e686d3baac3db Mon Sep 17 00:00:00 2001 From: DrKLO Date: Thu, 27 Mar 2014 00:16:28 +0400 Subject: [PATCH] Fixed photo sharing on devices with low memory --- TMessagesProj/build.gradle | 2 +- .../telegram/messenger/LocaleController.java | 5 +- .../telegram/messenger/MediaController.java | 36 +++--- .../telegram/messenger/MessagesStorage.java | 105 ++++++++++++++++++ .../java/org/telegram/ui/ChatActivity.java | 104 +++++++++++++++-- .../org/telegram/ui/ChatProfileActivity.java | 10 +- .../org/telegram/ui/GroupCreateActivity.java | 9 +- .../telegram/ui/GroupCreateFinalActivity.java | 75 ++++++++++++- .../java/org/telegram/ui/LaunchActivity.java | 65 +++++++++++ .../org/telegram/ui/SettingsActivity.java | 17 ++- .../ui/SettingsNotificationsActivity.java | 8 +- .../ui/SettingsWallpapersActivity.java | 26 ++++- .../org/telegram/ui/UserProfileActivity.java | 10 +- .../org/telegram/ui/Views/AvatarUpdater.java | 8 +- .../org/telegram/ui/Views/BaseFragment.java | 29 ++++- 15 files changed, 447 insertions(+), 62 deletions(-) diff --git a/TMessagesProj/build.gradle b/TMessagesProj/build.gradle index a4db15d09..b47f1c750 100644 --- a/TMessagesProj/build.gradle +++ b/TMessagesProj/build.gradle @@ -82,7 +82,7 @@ android { defaultConfig { minSdkVersion 8 targetSdkVersion 19 - versionCode 212 + versionCode 213 versionName "1.4.9" } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/LocaleController.java b/TMessagesProj/src/main/java/org/telegram/messenger/LocaleController.java index 80f3a2947..49b15d1c6 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/LocaleController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/LocaleController.java @@ -26,6 +26,7 @@ import java.util.Locale; public class LocaleController { public static boolean isRTL = false; + private static boolean is24HourFormat = false; public static FastDateFormat formatterDay; public static FastDateFormat formatterWeek; public static FastDateFormat formatterMonth; @@ -121,6 +122,7 @@ public class LocaleController { sortedLanguages.add(0, localeInfo); systemDefaultLocale = Locale.getDefault(); + is24HourFormat = DateFormat.is24HourFormat(ApplicationLoader.applicationContext); LocaleInfo currentInfo = null; boolean override = false; @@ -226,6 +228,7 @@ public class LocaleController { if (changingConfiguration) { return; } + is24HourFormat = DateFormat.is24HourFormat(ApplicationLoader.applicationContext); systemDefaultLocale = newConfig.locale; if (languageOverride != null) { LocaleInfo toSet = currentLocaleInfo; @@ -324,7 +327,7 @@ public class LocaleController { formatterWeek = FastDateFormat.getInstance("EEE", locale); if (lang != null) { - if (DateFormat.is24HourFormat(ApplicationLoader.applicationContext)) { + if (is24HourFormat) { formatterDay = FastDateFormat.getInstance("HH:mm", locale); } else { if (lang.toLowerCase().equals("ar")) { diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java index f58f7b087..ad707471f 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java @@ -831,26 +831,30 @@ public class MediaController implements NotificationCenter.NotificationCenterDel recordDialogId = dialog_id; fileBuffer.rewind(); - /*if (android.os.Build.VERSION.SDK_INT >= 16) { some devices crash with it - AutomaticGainControl agc = null; - try { - if (AutomaticGainControl.isAvailable()) { - agc = AutomaticGainControl.create(audioRecorder.getAudioSessionId()); - agc.setEnabled(true); - audioGainObj = agc; - } - } catch (Exception e) { + if (android.os.Build.VERSION.SDK_INT >= 16) { + File f = new File("/vendor/lib/libaudioeffect_jni.so"); + File f2 = new File("/system/lib/libaudioeffect_jni.so"); + if (f.exists() || f2.exists()) { + AutomaticGainControl agc = null; try { - if (agc != null) { - agc.release(); - agc = null; + if (AutomaticGainControl.isAvailable()) { + agc = AutomaticGainControl.create(audioRecorder.getAudioSessionId()); + agc.setEnabled(true); + audioGainObj = agc; } - } catch (Exception e2) { - FileLog.e("tmessages", e2); + } catch (Exception e) { + try { + if (agc != null) { + agc.release(); + agc = null; + } + } catch (Exception e2) { + FileLog.e("tmessages", e2); + } + FileLog.e("tmessages", e); } - FileLog.e("tmessages", e); } - }*/ + } audioRecorder.startRecording(); } catch (Exception e) { diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesStorage.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesStorage.java index 446e37ae9..91ebc6b68 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesStorage.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesStorage.java @@ -2825,4 +2825,109 @@ public class MessagesStorage { } }); } + + public TLRPC.User getUser(final int user_id) { + TLRPC.User user = null; + try { + SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid = %d", user_id)); + if (cursor.next()) { + byte[] userData = cursor.byteArrayValue(0); + if (userData != null) { + SerializedData data = new SerializedData(userData); + user = (TLRPC.User) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); + if (user != null) { + if (user.status != null) { + user.status.expires = cursor.intValue(1); + } + } + } + } + cursor.dispose(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + return user; + } + + public ArrayList getUsers(final ArrayList uids, final boolean[] error) { + ArrayList users = new ArrayList(); + try { + String uidsStr = ""; + + for (Integer uid : uids) { + if (uidsStr.length() != 0) { + uidsStr += ","; + } + uidsStr += uid; + } + + SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN (%s)", uidsStr)); + while (cursor.next()) { + byte[] userData = cursor.byteArrayValue(0); + if (userData != null) { + SerializedData data = new SerializedData(userData); + TLRPC.User user = (TLRPC.User) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); + if (user != null) { + if (user.status != null) { + user.status.expires = cursor.intValue(1); + } + users.add(user); + } else { + error[0] = true; + break; + } + } else { + error[0] = true; + break; + } + } + cursor.dispose(); + } catch (Exception e) { + error[0] = true; + FileLog.e("tmessages", e); + } + return users; + } + + public TLRPC.Chat getChat(final int chat_id) { + TLRPC.Chat chat = null; + try { + SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid = %d", chat_id)); + if (cursor.next()) { + byte[] chatData = cursor.byteArrayValue(0); + if (chatData != null) { + SerializedData data = new SerializedData(chatData); + chat = (TLRPC.Chat) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); + } + } + cursor.dispose(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + return chat; + } + + public TLRPC.EncryptedChat getEncryptedChat(final int chat_id) { + TLRPC.EncryptedChat chat = null; + try { + SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl FROM enc_chats WHERE uid = %d", chat_id)); + if (cursor.next()) { + byte[] chatData = cursor.byteArrayValue(0); + if (chatData != null) { + SerializedData data = new SerializedData(chatData); + chat = (TLRPC.EncryptedChat) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); + if (chat != null) { + chat.user_id = cursor.intValue(1); + chat.a_or_b = cursor.byteArrayValue(2); + chat.auth_key = cursor.byteArrayValue(3); + chat.ttl = cursor.intValue(4); + } + } + } + cursor.dispose(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + return chat; + } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 2a4eabbd5..40a2ab21d 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -104,6 +104,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Locale; +import java.util.concurrent.Semaphore; public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLayout.SizeNotifierRelativeLayoutDelegate, NotificationCenter.NotificationCenterDelegate, MessagesActivity.MessagesActivityDelegate, DocumentSelectActivity.DocumentSelectActivityDelegate { private LayoutListView chatListView; @@ -288,31 +289,99 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa @Override public boolean onFragmentCreate() { super.onFragmentCreate(); - int chatId = getArguments().getInt("chat_id", 0); - int userId = getArguments().getInt("user_id", 0); - int encId = getArguments().getInt("enc_id", 0); + final int chatId = getArguments().getInt("chat_id", 0); + final int userId = getArguments().getInt("user_id", 0); + final int encId = getArguments().getInt("enc_id", 0); if (chatId != 0) { currentChat = MessagesController.getInstance().chats.get(chatId); if (currentChat == null) { - return false; + final Semaphore semaphore = new Semaphore(0); + MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() { + @Override + public void run() { + currentChat = MessagesStorage.getInstance().getChat(chatId); + semaphore.release(); + } + }); + try { + semaphore.acquire(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + if (currentChat != null) { + MessagesController.getInstance().chats.put(currentChat.id, currentChat); + } else { + return false; + } } MessagesController.getInstance().loadChatInfo(currentChat.id); dialog_id = -chatId; } else if (userId != 0) { currentUser = MessagesController.getInstance().users.get(userId); if (currentUser == null) { - return false; + final Semaphore semaphore = new Semaphore(0); + MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() { + @Override + public void run() { + currentUser = MessagesStorage.getInstance().getUser(userId); + semaphore.release(); + } + }); + try { + semaphore.acquire(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + if (currentUser != null) { + MessagesController.getInstance().users.putIfAbsent(currentUser.id, currentUser); + } else { + return false; + } } dialog_id = userId; } else if (encId != 0) { currentEncryptedChat = MessagesController.getInstance().encryptedChats.get(encId); if (currentEncryptedChat == null) { - return false; + final Semaphore semaphore = new Semaphore(0); + MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() { + @Override + public void run() { + currentEncryptedChat = MessagesStorage.getInstance().getEncryptedChat(encId); + semaphore.release(); + } + }); + try { + semaphore.acquire(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + if (currentEncryptedChat != null) { + MessagesController.getInstance().encryptedChats.putIfAbsent(currentEncryptedChat.id, currentEncryptedChat); + } else { + return false; + } } currentUser = MessagesController.getInstance().users.get(currentEncryptedChat.user_id); if (currentUser == null) { - return false; + final Semaphore semaphore = new Semaphore(0); + MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() { + @Override + public void run() { + currentUser = MessagesStorage.getInstance().getUser(currentEncryptedChat.user_id); + semaphore.release(); + } + }); + try { + semaphore.acquire(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + if (currentUser != null) { + MessagesController.getInstance().users.putIfAbsent(currentUser.id, currentUser); + } else { + return false; + } } dialog_id = ((long)encId) << 32; maxMessageId = Integer.MIN_VALUE; @@ -1400,8 +1469,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa } @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == 0) { Utilities.addMediaToGallery(currentPicturePath); @@ -1450,6 +1518,18 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa } } + @Override + public void saveSelfArgs(Bundle args) { + if (currentPicturePath != null) { + args.putString("path", currentPicturePath); + } + } + + @Override + public void restoreSelfArgs(Bundle args) { + currentPicturePath = args.getString("path"); + } + public boolean processSendingText(String text) { text = text.replaceAll("\n\n+", "\n\n"); text = text.replaceAll(" +", " "); @@ -3104,7 +3184,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); currentPicturePath = image.getAbsolutePath(); } - startActivityForResult(takePictureIntent, 0); + parentActivity.startActivityForResult(takePictureIntent, 0); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -3114,7 +3194,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa try { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); - startActivityForResult(photoPickerIntent, 1); + parentActivity.startActivityForResult(photoPickerIntent, 1); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -3138,7 +3218,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa Intent chooserIntent = Intent.createChooser(pickIntent, ""); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takeVideoIntent }); - startActivityForResult(chooserIntent, 2); + parentActivity.startActivityForResult(chooserIntent, 2); } catch (Exception e) { FileLog.e("tmessages", e); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java index 0d845f2b0..5043ea42d 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java @@ -163,6 +163,9 @@ public class ChatProfileActivity extends BaseFragment implements NotificationCen editor.commit(); listView.invalidateViews(); } else if (i == 3) { + if (parentActivity == null) { + return; + } try { Intent tmpIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); tmpIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); @@ -187,7 +190,7 @@ public class ChatProfileActivity extends BaseFragment implements NotificationCen } tmpIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentSound); - startActivityForResult(tmpIntent, 15); + parentActivity.startActivityForResult(tmpIntent, 3); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -247,11 +250,10 @@ public class ChatProfileActivity extends BaseFragment implements NotificationCen } @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { avatarUpdater.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { - if (requestCode == 15) { + if (requestCode == 3) { Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); String name = null; if (ringtone != null && parentActivity != null) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateActivity.java index 5776b0a04..76f984efc 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateActivity.java @@ -431,11 +431,12 @@ public class GroupCreateActivity extends BaseFragment implements NotificationCen if (!selectedContacts.isEmpty()) { ArrayList result = new ArrayList(); result.addAll(selectedContacts.keySet()); - NotificationCenter.getInstance().addToMemCache(2, result); - } else { - return; + Bundle args = new Bundle(); + args.putIntegerArrayList("result", result); + GroupCreateFinalActivity fragment = new GroupCreateFinalActivity(); + fragment.setArguments(args); + ((LaunchActivity)parentActivity).presentFragment(fragment, "group_craate_final", false); } - ((LaunchActivity)parentActivity).presentFragment(new GroupCreateFinalActivity(), "group_craate_final", false); } }); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java index 79438d064..23c6c091a 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java @@ -28,6 +28,7 @@ import android.widget.TextView; import org.telegram.messenger.ConnectionsManager; import org.telegram.messenger.LocaleController; +import org.telegram.messenger.MessagesStorage; import org.telegram.messenger.TLRPC; import org.telegram.messenger.FileLog; import org.telegram.messenger.MessagesController; @@ -42,10 +43,11 @@ import org.telegram.ui.Views.PinnedHeaderListView; import org.telegram.ui.Views.SectionedBaseAdapter; import java.util.ArrayList; +import java.util.concurrent.Semaphore; public class GroupCreateFinalActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate, AvatarUpdater.AvatarUpdaterDelegate { private PinnedHeaderListView listView; - private TextView nameTextView; + private EditText nameTextView; private TLRPC.FileLocation avatar; private TLRPC.InputFile uploadedAvatar; private ArrayList selectedContacts; @@ -54,6 +56,7 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati private boolean donePressed; private AvatarUpdater avatarUpdater = new AvatarUpdater(); private ProgressDialog progressDialog = null; + private String nameToSet = null; @SuppressWarnings("unchecked") @Override @@ -64,7 +67,40 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati NotificationCenter.getInstance().addObserver(this, MessagesController.chatDidFailCreate); avatarUpdater.parentFragment = this; avatarUpdater.delegate = this; - selectedContacts = (ArrayList)NotificationCenter.getInstance().getFromMemCache(2); + selectedContacts = getArguments().getIntegerArrayList("result"); + final ArrayList usersToLoad = new ArrayList(); + for (Integer uid : selectedContacts) { + if (MessagesController.getInstance().users.get(uid) == null) { + usersToLoad.add(uid); + } + } + if (!usersToLoad.isEmpty()) { + final Semaphore semaphore = new Semaphore(0); + final ArrayList users = new ArrayList(); + final boolean[] error = new boolean[1]; + MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() { + @Override + public void run() { + users.addAll(MessagesStorage.getInstance().getUsers(usersToLoad, error)); + semaphore.release(); + } + }); + try { + semaphore.acquire(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + if (error[0]) { + return false; + } + if (!users.isEmpty()) { + for (TLRPC.User user : users) { + MessagesController.getInstance().users.putIfAbsent(user.id, user); + } + } else { + return false; + } + } return true; } @@ -125,6 +161,10 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati nameTextView = (EditText)fragmentView.findViewById(R.id.bubble_input_text); nameTextView.setHint(LocaleController.getString("EnterGroupNamePlaceholder", R.string.EnterGroupNamePlaceholder)); + if (nameToSet != null) { + nameTextView.setText(nameToSet); + nameToSet = null; + } listView = (PinnedHeaderListView)fragmentView.findViewById(R.id.listView); listView.setAdapter(new ListAdapter(parentActivity)); } else { @@ -200,11 +240,38 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati } @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { avatarUpdater.onActivityResult(requestCode, resultCode, data); } + @Override + public void saveSelfArgs(Bundle args) { + if (avatarUpdater != null && avatarUpdater.currentPicturePath != null) { + args.putString("path", avatarUpdater.currentPicturePath); + } + if (nameTextView != null) { + String text = nameTextView.getText().toString(); + if (text != null && text.length() != 0) { + args.putString("nameTextView", text); + } + } + } + + @Override + public void restoreSelfArgs(Bundle args) { + if (avatarUpdater != null) { + avatarUpdater.currentPicturePath = args.getString("path"); + } + String text = args.getString("nameTextView"); + if (text != null) { + if (nameTextView != null) { + nameTextView.setText(text); + } else { + nameToSet = text; + } + } + } + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.group_create_menu, menu); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index 0b003f29d..f6e7b10a4 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -140,6 +140,46 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen MessagesActivity fragment = new MessagesActivity(); fragment.onFragmentCreate(); ApplicationLoader.fragmentsStack.add(fragment); + + try { + if (savedInstanceState != null) { + String fragmentName = savedInstanceState.getString("fragment"); + if (fragmentName != null) { + Bundle args = savedInstanceState.getBundle("args"); + if (fragmentName.equals("chat")) { + if (args != null) { + ChatActivity chat = new ChatActivity(); + chat.setArguments(args); + if (chat.onFragmentCreate()) { + ApplicationLoader.fragmentsStack.add(chat); + chat.restoreSelfArgs(savedInstanceState); + } + } + } else if (fragmentName.equals("settings")) { + SettingsActivity settings = new SettingsActivity(); + settings.onFragmentCreate(); + settings.restoreSelfArgs(savedInstanceState); + ApplicationLoader.fragmentsStack.add(settings); + } else if (fragmentName.equals("group")) { + if (args != null) { + GroupCreateFinalActivity group = new GroupCreateFinalActivity(); + group.setArguments(args); + if (group.onFragmentCreate()) { + group.restoreSelfArgs(savedInstanceState); + ApplicationLoader.fragmentsStack.add(group); + } + } + } else if (fragmentName.equals("wallpapers")) { + SettingsWallpapersActivity settings = new SettingsWallpapersActivity(); + settings.onFragmentCreate(); + settings.restoreSelfArgs(savedInstanceState); + ApplicationLoader.fragmentsStack.add(settings); + } + } + } + } catch (Exception e) { + FileLog.e("tmessages", e); + } } handleIntent(getIntent(), false, savedInstanceState != null); @@ -536,6 +576,15 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen } } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (ApplicationLoader.fragmentsStack.size() != 0) { + BaseFragment fragment = ApplicationLoader.fragmentsStack.get(ApplicationLoader.fragmentsStack.size() - 1); + fragment.onActivityResultFragment(requestCode, resultCode, data); + } + } + @Override protected void onPause() { super.onPause(); @@ -932,6 +981,22 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen protected void onSaveInstanceState(Bundle outState) { try { super.onSaveInstanceState(outState); + if (!ApplicationLoader.fragmentsStack.isEmpty()) { + BaseFragment lastFragment = ApplicationLoader.fragmentsStack.get(ApplicationLoader.fragmentsStack.size() - 1); + Bundle args = lastFragment.getArguments(); + if (lastFragment instanceof ChatActivity && args != null) { + outState.putBundle("args", args); + outState.putString("fragment", "chat"); + } else if (lastFragment instanceof SettingsActivity) { + outState.putString("fragment", "settings"); + } else if (lastFragment instanceof GroupCreateFinalActivity && args != null) { + outState.putBundle("args", args); + outState.putString("fragment", "group"); + } else if (lastFragment instanceof SettingsWallpapersActivity) { + outState.putString("fragment", "wallpapers"); + } + lastFragment.saveSelfArgs(outState); + } } catch (Exception e) { FileLog.e("tmessages", e); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java index 53e3be4fe..508536041 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java @@ -445,11 +445,24 @@ public class SettingsActivity extends BaseFragment implements NotificationCenter } @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { avatarUpdater.onActivityResult(requestCode, resultCode, data); } + @Override + public void saveSelfArgs(Bundle args) { + if (avatarUpdater != null && avatarUpdater.currentPicturePath != null) { + args.putString("path", avatarUpdater.currentPicturePath); + } + } + + @Override + public void restoreSelfArgs(Bundle args) { + if (avatarUpdater != null) { + avatarUpdater.currentPicturePath = args.getString("path"); + } + } + @Override public void didReceivedNotification(int id, Object... args) { if (id == MessagesController.updateInterfaces) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java index b72101955..d60750512 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java @@ -103,6 +103,9 @@ public class SettingsNotificationsActivity extends BaseFragment { editor.commit(); listView.invalidateViews(); } else if (i == 4 || i == 9) { + if (parentActivity == null) { + return; + } try { SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); Intent tmpIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); @@ -137,7 +140,7 @@ public class SettingsNotificationsActivity extends BaseFragment { } } tmpIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentSound); - startActivityForResult(tmpIntent, i); + parentActivity.startActivityForResult(tmpIntent, i); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -228,8 +231,7 @@ public class SettingsNotificationsActivity extends BaseFragment { } @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); String name = null; diff --git a/TMessagesProj/src/main/java/org/telegram/ui/SettingsWallpapersActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/SettingsWallpapersActivity.java index f294315ed..9dc7f6007 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/SettingsWallpapersActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/SettingsWallpapersActivity.java @@ -113,6 +113,9 @@ public class SettingsWallpapersActivity extends BaseFragment implements Notifica builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { + if (parentActivity == null) { + return; + } if (i == 0) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File image = Utilities.generatePicturePath(); @@ -120,11 +123,11 @@ public class SettingsWallpapersActivity extends BaseFragment implements Notifica takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); currentPicturePath = image.getAbsolutePath(); } - startActivityForResult(takePictureIntent, 0); + parentActivity.startActivityForResult(takePictureIntent, 10); } else if (i == 1) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); - startActivityForResult(photoPickerIntent, 1); + parentActivity.startActivityForResult(photoPickerIntent, 11); } } }); @@ -194,10 +197,9 @@ public class SettingsWallpapersActivity extends BaseFragment implements Notifica } @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { - if (requestCode == 0) { + if (requestCode == 10) { Utilities.addMediaToGallery(currentPicturePath); try { Bitmap bitmap = FileLoader.loadBitmap(currentPicturePath, null, Utilities.dp(320), Utilities.dp(480)); @@ -211,7 +213,7 @@ public class SettingsWallpapersActivity extends BaseFragment implements Notifica FileLog.e("tmessages", e); } currentPicturePath = null; - } else if (requestCode == 1) { + } else if (requestCode == 11) { Uri imageUri = data.getData(); Cursor cursor = parentActivity.getContentResolver().query(imageUri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); if (cursor == null) { @@ -239,6 +241,18 @@ public class SettingsWallpapersActivity extends BaseFragment implements Notifica } } + @Override + public void saveSelfArgs(Bundle args) { + if (currentPicturePath != null) { + args.putString("path", currentPicturePath); + } + } + + @Override + public void restoreSelfArgs(Bundle args) { + currentPicturePath = args.getString("path"); + } + private void processSelectedBackground() { TLRPC.WallPaper wallPaper = wallpappersByIds.get(selectedBackground); if (selectedBackground != -1 && selectedBackground != 1000001 && wallPaper != null && wallPaper instanceof TLRPC.TL_wallPaper) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/UserProfileActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/UserProfileActivity.java index 41993d46a..675fd3e9e 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/UserProfileActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/UserProfileActivity.java @@ -144,6 +144,9 @@ public class UserProfileActivity extends BaseFragment implements NotificationCen } else if (i == 5 && dialog_id == 0 || dialog_id != 0 && (i == 7 && currentEncryptedChat instanceof TLRPC.TL_encryptedChat || i == 5 && !(currentEncryptedChat instanceof TLRPC.TL_encryptedChat))) { + if (parentActivity == null) { + return; + } try { Intent tmpIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); tmpIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); @@ -168,7 +171,7 @@ public class UserProfileActivity extends BaseFragment implements NotificationCen } tmpIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentSound); - startActivityForResult(tmpIntent, 0); + parentActivity.startActivityForResult(tmpIntent, 12); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -256,8 +259,7 @@ public class UserProfileActivity extends BaseFragment implements NotificationCen } @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (data == null) { return; @@ -279,7 +281,7 @@ public class UserProfileActivity extends BaseFragment implements NotificationCen SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); - if (requestCode == 0) { + if (requestCode == 12) { if (name != null && ringtone != null) { editor.putString("sound_" + user_id, name); editor.putString("sound_path_" + user_id, ringtone.toString()); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java b/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java index 304606d69..e3bdab82c 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java @@ -58,7 +58,7 @@ public class AvatarUpdater implements NotificationCenter.NotificationCenterDeleg takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); currentPicturePath = image.getAbsolutePath(); } - parentFragment.startActivityForResult(takePictureIntent, 0); + parentFragment.parentActivity.startActivityForResult(takePictureIntent, 13); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -68,7 +68,7 @@ public class AvatarUpdater implements NotificationCenter.NotificationCenterDeleg try { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); - parentFragment.startActivityForResult(photoPickerIntent, 1); + parentFragment.parentActivity.startActivityForResult(photoPickerIntent, 14); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -102,12 +102,12 @@ public class AvatarUpdater implements NotificationCenter.NotificationCenterDeleg public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { - if (requestCode == 0) { + if (requestCode == 13) { Utilities.addMediaToGallery(currentPicturePath); startCrop(currentPicturePath, null); currentPicturePath = null; - } else if (requestCode == 1) { + } else if (requestCode == 14) { if (data == null || data.getData() == null) { return; } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Views/BaseFragment.java b/TMessagesProj/src/main/java/org/telegram/ui/Views/BaseFragment.java index 557552dba..4ab44f30c 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Views/BaseFragment.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Views/BaseFragment.java @@ -8,6 +8,7 @@ package org.telegram.ui.Views; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; @@ -28,6 +29,7 @@ public class BaseFragment extends Fragment { public int classGuid = 0; public boolean firstStart = true; public boolean animationInProgress = false; + private long currentAnimationDuration = 0; private boolean removeParentOnDestroy = false; private boolean removeParentOnAnimationEnd = true; @@ -108,6 +110,16 @@ public class BaseFragment extends Fragment { public void onAnimationStart() { animationInProgress = true; + if (fragmentView != null) { + fragmentView.postDelayed(new Runnable() { + @Override + public void run() { + if (animationInProgress) { + onAnimationEnd(); + } + } + }, currentAnimationDuration); + } } public void onAnimationEnd() { @@ -137,6 +149,7 @@ public class BaseFragment extends Fragment { public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { if (nextAnim != 0) { Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim); + currentAnimationDuration = anim.getDuration(); anim.setAnimationListener(new Animation.AnimationListener() { @@ -149,7 +162,9 @@ public class BaseFragment extends Fragment { } public void onAnimationEnd(Animation animation) { - BaseFragment.this.onAnimationEnd(); + if (animationInProgress) { + BaseFragment.this.onAnimationEnd(); + } } }); @@ -166,4 +181,16 @@ public class BaseFragment extends Fragment { public void applySelfActionBar() { } + + public void onActivityResultFragment(int requestCode, int resultCode, Intent data) { + + } + + public void saveSelfArgs(Bundle args) { + + } + + public void restoreSelfArgs(Bundle args) { + + } }