Merge pull request #140 from kiwix/135

WiFi only preference added
This commit is contained in:
Isaac Hutt 2017-05-20 18:10:09 +02:00 committed by GitHub
commit 354b34e7c0
9 changed files with 92 additions and 38 deletions

View File

@ -181,6 +181,8 @@ public class KiwixMobileActivity extends BaseActivity implements WebViewCallback
public static boolean refresh; public static boolean refresh;
public static boolean wifiOnly;
private static Uri KIWIX_LOCAL_MARKET_URI; private static Uri KIWIX_LOCAL_MARKET_URI;
private static Uri KIWIX_BROWSER_MARKET_URI; private static Uri KIWIX_BROWSER_MARKET_URI;
@ -303,6 +305,7 @@ public class KiwixMobileActivity extends BaseActivity implements WebViewCallback
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
nightMode = sharedPreferences.getBoolean(PREF_NIGHT_MODE, false); nightMode = sharedPreferences.getBoolean(PREF_NIGHT_MODE, false);
wifiOnly = sharedPreferences.getBoolean(KiwixSettingsActivity.PREF_WIFI_ONLY, true);
if (nightMode) { if (nightMode) {
setTheme(R.style.AppTheme_Night); setTheme(R.style.AppTheme_Night);
} }

View File

@ -3,9 +3,12 @@ package org.kiwix.kiwixmobile.downloader;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
@ -25,6 +28,9 @@ import android.widget.TextView;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import org.kiwix.kiwixmobile.KiwixMobileActivity;
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
import org.kiwix.kiwixmobile.utils.NetworkUtils;
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment; import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment;
@ -76,6 +82,22 @@ public class DownloadFragment extends Fragment {
} }
} }
public static void showNoWiFiWarning(Context context, Runnable yesAction) {
new AlertDialog.Builder(context)
.setTitle(R.string.wifi_only_title)
.setMessage(R.string.wifi_only_msg)
.setPositiveButton(R.string.yes, (dialog, i) -> {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean(KiwixSettingsActivity.PREF_WIFI_ONLY, false)
.apply();
KiwixMobileActivity.wifiOnly = false;
yesAction.run();
})
.setNegativeButton(R.string.no, (dialog, i) -> {})
.show();
}
public class DownloadAdapter extends BaseAdapter { public class DownloadAdapter extends BaseAdapter {
private LinkedHashMap<Integer, LibraryNetworkEntity.Book> mData = new LinkedHashMap<Integer, LibraryNetworkEntity.Book>(); private LinkedHashMap<Integer, LibraryNetworkEntity.Book> mData = new LinkedHashMap<Integer, LibraryNetworkEntity.Book>();
@ -141,8 +163,8 @@ public class DownloadFragment extends Fragment {
private void setPlayState(ImageView pauseButton, int position, int newPlayState) { private void setPlayState(ImageView pauseButton, int position, int newPlayState) {
if(newPlayState == DownloadService.PLAY) { //Playing if(newPlayState == DownloadService.PLAY) { //Playing
LibraryFragment.mService.playDownload(mKeys[position]); if (LibraryFragment.mService.playDownload(mKeys[position]))
pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp));
} else { //Pausing } else { //Pausing
LibraryFragment.mService.pauseDownload(mKeys[position]); LibraryFragment.mService.pauseDownload(mKeys[position]);
pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp));
@ -185,6 +207,12 @@ public class DownloadFragment extends Fragment {
pause.setOnClickListener(v -> { pause.setOnClickListener(v -> {
int newPlayPauseState = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY ? DownloadService.PAUSE : DownloadService.PLAY; int newPlayPauseState = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY ? DownloadService.PAUSE : DownloadService.PLAY;
if (newPlayPauseState == DownloadService.PLAY && KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getContext())) {
showNoWiFiWarning(getContext(), () -> {setPlayState(pause, position, newPlayPauseState);});
return;
}
setPlayState(pause, position, newPlayPauseState); setPlayState(pause, position, newPlayPauseState);
}); });

View File

@ -2,6 +2,7 @@ package org.kiwix.kiwixmobile.downloader;
import static org.kiwix.kiwixmobile.utils.files.FileUtils.getCurrentSize; import static org.kiwix.kiwixmobile.utils.files.FileUtils.getCurrentSize;
import android.app.AlertDialog;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
@ -14,6 +15,7 @@ import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.util.Pair; import android.util.Pair;
import android.util.SparseArray; import android.util.SparseArray;
import android.util.SparseIntArray; import android.util.SparseIntArray;
@ -30,7 +32,9 @@ import okhttp3.Response;
import okio.BufferedSource; import okio.BufferedSource;
import org.kiwix.kiwixmobile.KiwixApplication; import org.kiwix.kiwixmobile.KiwixApplication;
import org.kiwix.kiwixmobile.KiwixMobileActivity; import org.kiwix.kiwixmobile.KiwixMobileActivity;
import org.kiwix.kiwixmobile.utils.NetworkUtils;
import org.kiwix.kiwixmobile.utils.TestingUtils; import org.kiwix.kiwixmobile.utils.TestingUtils;
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity;
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment; import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.database.BookDao; import org.kiwix.kiwixmobile.database.BookDao;
@ -58,6 +62,7 @@ public class DownloadService extends Service {
public static final int CANCEL = 4; public static final int CANCEL = 4;
public static final String ACTION_PAUSE = "PAUSE"; public static final String ACTION_PAUSE = "PAUSE";
public static final String ACTION_STOP = "STOP"; public static final String ACTION_STOP = "STOP";
public static final String ACTION_NO_WIFI = "NO_WIFI";
public static final String NOTIFICATION_ID = "NOTIFICATION_ID"; public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
public static int notificationCount = 1; public static int notificationCount = 1;
public static ArrayList<String> notifications = new ArrayList<>(); public static ArrayList<String> notifications = new ArrayList<>();
@ -103,7 +108,11 @@ public class DownloadService extends Service {
return START_NOT_STICKY; return START_NOT_STICKY;
} }
if (intent.hasExtra(NOTIFICATION_ID) && intent.getAction().equals(ACTION_PAUSE)) { if (intent.hasExtra(NOTIFICATION_ID) && intent.getAction().equals(ACTION_PAUSE)) {
toggleDownload(intent.getIntExtra(NOTIFICATION_ID, 0)); if (KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getApplicationContext())) {
startActivity(new Intent(this, ZimManageActivity.class).setAction(ACTION_NO_WIFI).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
this.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
} else
toggleDownload(intent.getIntExtra(NOTIFICATION_ID, 0));
return START_NOT_STICKY; return START_NOT_STICKY;
} }
@ -201,7 +210,7 @@ public class DownloadService extends Service {
downloadFragment.listView.invalidateViews(); downloadFragment.listView.invalidateViews();
} }
public void playDownload(int notificationID) { public boolean playDownload(int notificationID) {
downloadStatus.put(notificationID, PLAY); downloadStatus.put(notificationID, PLAY);
synchronized (pauseLock) { synchronized (pauseLock) {
pauseLock.notify(); pauseLock.notify();
@ -211,6 +220,8 @@ public class DownloadService extends Service {
notificationManager.notify(notificationID, notification.get(notificationID).build()); notificationManager.notify(notificationID, notification.get(notificationID).build());
downloadFragment.downloadAdapter.notifyDataSetChanged(); downloadFragment.downloadAdapter.notifyDataSetChanged();
downloadFragment.listView.invalidateViews(); downloadFragment.listView.invalidateViews();
return true;
} }
private void downloadBook(String url, int notificationID, LibraryNetworkEntity.Book book) { private void downloadBook(String url, int notificationID, LibraryNetworkEntity.Book book) {
@ -365,6 +376,10 @@ public class DownloadService extends Service {
attempts = timeout; attempts = timeout;
break; break;
} }
if (KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getApplicationContext()))
pauseDownload(chunk.getNotificationID());
if (downloadStatus.get(chunk.getNotificationID()) == PAUSE) { if (downloadStatus.get(chunk.getNotificationID()) == PAUSE) {
synchronized (pauseLock) { synchronized (pauseLock) {
try { try {

View File

@ -81,6 +81,8 @@ public class KiwixSettingsActivity extends AppCompatActivity {
public static final String PREF_HIDETOOLBAR = "pref_hidetoolbar"; public static final String PREF_HIDETOOLBAR = "pref_hidetoolbar";
public static final String PREF_WIFI_ONLY = "pref_wifi_only";
public static String zimFile; public static String zimFile;
public static boolean allHistoryCleared = false; public static boolean allHistoryCleared = false;
@ -266,6 +268,9 @@ public class KiwixSettingsActivity extends AppCompatActivity {
KiwixMobileActivity.nightMode = sharedPreferences.getBoolean(PREF_NIGHTMODE, false); KiwixMobileActivity.nightMode = sharedPreferences.getBoolean(PREF_NIGHTMODE, false);
getActivity().recreate(); getActivity().recreate();
} }
if (key.equals(PREF_WIFI_ONLY)) {
KiwixMobileActivity.wifiOnly = sharedPreferences.getBoolean(PREF_WIFI_ONLY, true);
}
} }

View File

@ -3,6 +3,7 @@ package org.kiwix.kiwixmobile.utils;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log; import android.util.Log;
import java.util.UUID; import java.util.UUID;
import org.kiwix.kiwixmobile.KiwixMobileActivity; import org.kiwix.kiwixmobile.KiwixMobileActivity;
@ -29,6 +30,21 @@ public class NetworkUtils {
return false; return false;
} }
public static boolean isWiFi(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null)
return false;
if (Build.VERSION.SDK_INT >= 23) {
NetworkInfo network = connectivity.getActiveNetworkInfo();
return network.getType() == ConnectivityManager.TYPE_WIFI;
} else {
NetworkInfo wifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifi.isConnected();
}
}
public static String getFileNameFromUrl(String url) { public static String getFileNameFromUrl(String url) {
int index = url.lastIndexOf('?'); int index = url.lastIndexOf('?');

View File

@ -30,6 +30,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.kiwix.kiwixmobile.KiwixMobileActivity; import org.kiwix.kiwixmobile.KiwixMobileActivity;
import org.kiwix.kiwixmobile.downloader.DownloadService;
import org.kiwix.kiwixmobile.library.LibraryAdapter.Language; import org.kiwix.kiwixmobile.library.LibraryAdapter.Language;
import org.kiwix.kiwixmobile.utils.TestingUtils; import org.kiwix.kiwixmobile.utils.TestingUtils;
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment; import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment;
@ -81,6 +82,10 @@ public class ZimManageActivity extends AppCompatActivity {
setUpToolbar(); setUpToolbar();
if (DownloadService.ACTION_NO_WIFI.equals(getIntent().getAction())) {
DownloadFragment.showNoWiFiWarning(this, () -> {});
}
// Create the adapter that will return a fragment for each of the three // Create the adapter that will return a fragment for each of the three
// primary sections of the activity. // primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

View File

@ -52,6 +52,7 @@ import org.kiwix.kiwixmobile.downloader.DownloadIntent;
import org.kiwix.kiwixmobile.downloader.DownloadService; import org.kiwix.kiwixmobile.downloader.DownloadService;
import org.kiwix.kiwixmobile.library.LibraryAdapter; import org.kiwix.kiwixmobile.library.LibraryAdapter;
import org.kiwix.kiwixmobile.network.KiwixService; import org.kiwix.kiwixmobile.network.KiwixService;
import org.kiwix.kiwixmobile.utils.NetworkUtils;
import org.kiwix.kiwixmobile.utils.StorageUtils; import org.kiwix.kiwixmobile.utils.StorageUtils;
import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.StyleUtils;
import org.kiwix.kiwixmobile.utils.TestingUtils; import org.kiwix.kiwixmobile.utils.TestingUtils;
@ -190,20 +191,6 @@ public class LibraryFragment extends Fragment
TestingUtils.unbindResource(LibraryFragment.class); TestingUtils.unbindResource(LibraryFragment.class);
} }
public void displayNetworkConfirmation() {
libraryList.removeFooterView(progressBar);
networkText.setText(R.string.download_over_network);
networkText.setVisibility(View.VISIBLE);
permissionButton.setVisibility(View.VISIBLE);
permissionButton.setOnClickListener(view -> {
presenter.loadBooks();
permissionButton.setVisibility(View.GONE);
networkText.setVisibility(View.GONE);
});
TestingUtils.unbindResource(LibraryFragment.class);
}
public void noNetworkConnection() { public void noNetworkConnection() {
displayNoNetworkConnection(); displayNoNetworkConnection();
} }
@ -259,24 +246,14 @@ public class LibraryFragment extends Fragment
return; return;
} }
if (isWiFi()) { if (KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getContext())) {
downloadFile((Book) parent.getAdapter().getItem(position)); DownloadFragment.showNoWiFiWarning(getContext(), () -> {downloadFile((Book) parent.getAdapter().getItem(position));});
} else { } else {
mobileDownloadDialog(position, parent); downloadFile((Book) parent.getAdapter().getItem(position));
} }
} }
} }
public boolean isWiFi() {
if (Build.VERSION.SDK_INT >= 23) {
NetworkInfo network = conMan.getActiveNetworkInfo();
return network.getType() == ConnectivityManager.TYPE_WIFI;
} else {
NetworkInfo wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifi.isConnected();
}
}
public void mobileDownloadDialog(int position, AdapterView<?> parent) { public void mobileDownloadDialog(int position, AdapterView<?> parent) {
new AlertDialog.Builder(super.getActivity(), dialogStyle()) new AlertDialog.Builder(super.getActivity(), dialogStyle())
@ -296,7 +273,7 @@ public class LibraryFragment extends Fragment
@Override @Override
public void downloadFile(Book book) { public void downloadFile(Book book) {
downloadingBooks.add(book); downloadingBooks.add(book);
if (libraryAdapter != null && faActivity.searchView != null) { if (libraryAdapter != null && faActivity != null && faActivity.searchView != null) {
libraryAdapter.getFilter().filter(faActivity.searchView.getQuery()); libraryAdapter.getFilter().filter(faActivity.searchView.getQuery());
} }
Toast.makeText(super.getActivity(), getString(R.string.download_started_library), Toast.LENGTH_LONG) Toast.makeText(super.getActivity(), getString(R.string.download_started_library), Toast.LENGTH_LONG)
@ -365,11 +342,9 @@ public class LibraryFragment extends Fragment
NetworkInfo network = conMan.getActiveNetworkInfo(); NetworkInfo network = conMan.getActiveNetworkInfo();
if ((books == null || books.isEmpty()) && network != null && network.isConnected()) { if ((books == null || books.isEmpty()) && network != null && network.isConnected()) {
if (isWiFi()) { presenter.loadBooks();
presenter.loadBooks(); permissionButton.setVisibility(View.GONE);
} else { networkText.setVisibility(View.GONE);
displayNetworkConfirmation();
}
} }
} }
} }

View File

@ -111,7 +111,6 @@
<string name="no_downloads_here">No downloads here</string> <string name="no_downloads_here">No downloads here</string>
<string name="no_files_here">No files here</string> <string name="no_files_here">No files here</string>
<string name="download_complete_snackbar">Download complete</string> <string name="download_complete_snackbar">Download complete</string>
<string name="download_over_network">Are you sure you want to start the download without WiFi?</string>
<string name="download_no_space">Insufficient space to download this file.</string> <string name="download_no_space">Insufficient space to download this file.</string>
<string name="space_available">Space Available:</string> <string name="space_available">Space Available:</string>
<string name="zim_simple">Simple</string> <string name="zim_simple">Simple</string>
@ -165,4 +164,7 @@
<string name="delete_tab">Delete Tab</string> <string name="delete_tab">Delete Tab</string>
<string name="next">Next</string> <string name="next">Next</string>
<string name="previous">Previous</string> <string name="previous">Previous</string>
<string name="wifi_only_title">Allow downloading content via mobile network?</string>
<string name="wifi_only_msg">If you choose "Yes" you won\'t be warned in future. However, you can always change this in Settings</string>
<string name="pref_wifi_only">Download content only via WiFi</string>
</resources> </resources>

View File

@ -38,6 +38,11 @@
android:key="pref_full_text_search" android:key="pref_full_text_search"
android:title="@string/pref_full_text_search_title" android:title="@string/pref_full_text_search_title"
android:summary="@string/pref_full_text_search_summary"/> android:summary="@string/pref_full_text_search_summary"/>
<org.kiwix.kiwixmobile.settings.CustomSwitchPreference
android:defaultValue="true"
android:key="pref_wifi_only"
android:title="@string/pref_wifi_only"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory