From d96ce3c140bd49c5c8c63cea2bc9afad8a16b8bb Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Tue, 29 Apr 2014 01:30:37 +0200 Subject: [PATCH] Added option for "only wifi automatic download" in audio and photo settings (issue #146) --- .../messenger/ConnectionsManager.java | 17 +++++++ .../java/org/telegram/ui/ChatActivity.java | 5 ++ .../org/telegram/ui/SettingsActivity.java | 46 +++++++++++++++++-- TMessagesProj/src/main/res/values/strings.xml | 1 + 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java index 2ea99793e94..37501da9336 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java @@ -16,9 +16,12 @@ import android.os.Build; import android.util.Base64; +import net.hockeyapp.android.utils.ConnectionManager; + import org.telegram.ui.ApplicationLoader; import java.io.File; +import java.sql.Connection; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -893,6 +896,20 @@ public static boolean isNetworkOnline() { return status; } + public static boolean isConnectionTypeWifi() { + boolean connectionType = false; + try { + ConnectivityManager cm = (ConnectivityManager)ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo mWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + if (mWifi != null && mWifi.isAvailable()) + connectionType = true; + } catch(Exception e) { + FileLog.e("tmessages", e); + return true; + } + return connectionType; + } + public int getCurrentTime() { return (int)(System.currentTimeMillis() / 1000) + timeDifference; } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 5c9744a2e47..805b56bb790 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -451,6 +451,11 @@ public void run() { } else { downloadAudios = preferences.getBoolean("audio_download_user", true); } + if (!ConnectionsManager.isConnectionTypeWifi()) { + downloadPhotos = !preferences.getBoolean("photo_only_wifi", false); + downloadAudios = !preferences.getBoolean("audio_only_wifi", false); + } + return true; } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java index 366b5704ab3..e0e28e1065d 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/SettingsActivity.java @@ -92,9 +92,11 @@ public class SettingsActivity extends BaseFragment implements NotificationCenter private int photoDownloadSection; private int photoDownloadChatRow; private int photoDownloadPrivateRow; + private int photoDownloadOnlyWifiRow; private int audioDownloadSection; private int audioDownloadChatRow; private int audioDownloadPrivateRow; + private int audioDownloadOnlyWifiRow; private int telegramFaqRow; private int languageRow; private int versionSectionRow; @@ -189,9 +191,11 @@ public void run() { photoDownloadSection = rowCount++; photoDownloadChatRow = rowCount++; photoDownloadPrivateRow = rowCount++; + photoDownloadOnlyWifiRow = rowCount++; audioDownloadSection = rowCount++; audioDownloadChatRow = rowCount++; audioDownloadPrivateRow = rowCount++; + audioDownloadOnlyWifiRow = rowCount++; messagesSectionRow = rowCount++; textSizeRow = rowCount++; sendByEnterRow = rowCount++; @@ -364,6 +368,24 @@ public void run(TLObject response, TLRPC.TL_error error) { if (listView != null) { listView.invalidateViews(); } + } else if (i == photoDownloadOnlyWifiRow) { + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE); + boolean value = preferences.getBoolean("photo_only_wifi", false); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("photo_only_wifi", !value); + editor.commit(); + if (listView != null) { + listView.invalidateViews(); + } + } else if (i == audioDownloadOnlyWifiRow) { + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE); + boolean value = preferences.getBoolean("audio_only_wifi", false); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("audio_only_wifi", !value); + editor.commit(); + if (listView != null) { + listView.invalidateViews(); + } } else if (i == audioDownloadChatRow) { SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE); boolean value = preferences.getBoolean("audio_download_chat", true); @@ -633,8 +655,8 @@ public boolean areAllItemsEnabled() { @Override public boolean isEnabled(int i) { return i == textSizeRow || i == enableAnimationsRow || i == connectionStateShowRow || i == blockedRow || i == notificationRow || i == backgroundRow || - i == askQuestionRow || i == sendLogsRow || i == sendByEnterRow || i == terminateSessionsRow || i == photoDownloadPrivateRow || - i == photoDownloadChatRow || i == clearLogsRow || i == audioDownloadChatRow || i == audioDownloadPrivateRow || i == languageRow || + i == askQuestionRow || i == sendLogsRow || i == sendByEnterRow || i == terminateSessionsRow || i == photoDownloadPrivateRow || i == audioDownloadOnlyWifiRow || + i == photoDownloadOnlyWifiRow || i == photoDownloadChatRow || i == clearLogsRow || i == audioDownloadChatRow || i == audioDownloadPrivateRow || i == languageRow || i == switchBackendButtonRow || i == telegramFaqRow || i == telegramVersionRow; } @@ -916,6 +938,24 @@ public void run() { } else { checkButton.setImageResource(R.drawable.btn_check_off); } + } else if (i == photoDownloadOnlyWifiRow) { + textView.setText(LocaleController.getString("AutomaticDownloadOnlyWifi", R.string.AutomaticDownloadOnlyWifi)); + divider.setVisibility(View.INVISIBLE); + boolean enabled = preferences.getBoolean("photo_only_wifi", false); + if (enabled) { + checkButton.setImageResource(R.drawable.btn_check_on); + } else { + checkButton.setImageResource(R.drawable.btn_check_off); + } + } else if (i == audioDownloadOnlyWifiRow) { + textView.setText(LocaleController.getString("AutomaticDownloadOnlyWifi", R.string.AutomaticDownloadOnlyWifi)); + divider.setVisibility(View.INVISIBLE); + boolean enabled = preferences.getBoolean("audio_only_wifi", false); + if (enabled) { + checkButton.setImageResource(R.drawable.btn_check_on); + } else { + checkButton.setImageResource(R.drawable.btn_check_off); + } } else if (i == audioDownloadChatRow) { textView.setText(LocaleController.getString("AutomaticPhotoDownloadGroups", R.string.AutomaticPhotoDownloadGroups)); divider.setVisibility(View.VISIBLE); @@ -1003,7 +1043,7 @@ public int getItemViewType(int i) { return 1; } else if (i == textSizeRow || i == languageRow) { return 5; - } else if (i == enableAnimationsRow || i == connectionStateShowRow|| i == sendByEnterRow || i == photoDownloadChatRow || i == photoDownloadPrivateRow || i == audioDownloadChatRow || i == audioDownloadPrivateRow) { + } else if (i == enableAnimationsRow || i == connectionStateShowRow|| i == sendByEnterRow || i == photoDownloadChatRow || i == photoDownloadPrivateRow || i == audioDownloadChatRow || i == audioDownloadPrivateRow || i == audioDownloadOnlyWifiRow || i == photoDownloadOnlyWifiRow) { return 3; } else if (i == numberRow || i == notificationRow || i == blockedRow || i == backgroundRow || i == askQuestionRow || i == sendLogsRow || i == terminateSessionsRow || i == clearLogsRow || i == switchBackendButtonRow || i == telegramFaqRow || i == telegramVersionRow) { return 2; diff --git a/TMessagesProj/src/main/res/values/strings.xml b/TMessagesProj/src/main/res/values/strings.xml index 0dc936228f5..ca37bc1a3fb 100644 --- a/TMessagesProj/src/main/res/values/strings.xml +++ b/TMessagesProj/src/main/res/values/strings.xml @@ -256,6 +256,7 @@ AUTOMATIC AUDIO DOWNLOAD Groups Private Chats + Only with WIFI EVENTS Contact joined Telegram PEBBLE