From 5602c3d9e60fd2df2a089d7a19c19a5f401e26b9 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 15 Dec 2017 11:12:24 +0000 Subject: [PATCH 1/5] clean up logic add no items report --- .../library_view/LibraryFragment.java | 73 ++++++++----------- .../library_view/LibraryPresenter.java | 2 +- .../library_view/LibraryViewCallback.java | 2 + app/src/main/res/values/strings.xml | 1 + 4 files changed, 35 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java index 75d147eb2..cd1b39f0f 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java @@ -57,6 +57,7 @@ import butterknife.ButterKnife; import eu.mhutti1.utils.storage.StorageDevice; import eu.mhutti1.utils.storage.support.StorageSelectDialog; +import static android.view.View.GONE; import static org.kiwix.kiwixmobile.downloader.DownloadService.KIWIX_ROOT; import static org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; @@ -77,7 +78,8 @@ public class LibraryFragment extends Fragment public LinearLayout llLayout; - public SwipeRefreshLayout swipeRefreshLayout; + @BindView(R.id.library_swiperefresh) + SwipeRefreshLayout swipeRefreshLayout; private ArrayList books = new ArrayList<>(); @@ -110,47 +112,25 @@ public class LibraryFragment extends Fragment public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - setupDagger(); TestingUtils.bindResource(LibraryFragment.class); - faActivity = (ZimManageActivity) super.getActivity(); - - // Replace LinearLayout by the type of the root element of the layout you're trying to load - llLayout = (LinearLayout) inflater.inflate(R.layout.activity_library, container, false); ButterKnife.bind(this, llLayout); - - // SwipeRefreshLayout for the list view - swipeRefreshLayout = (SwipeRefreshLayout) llLayout.findViewById(R.id.library_swiperefresh); - swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { - @Override - public void onRefresh() { - refreshFragment(); - } - }); - - displayScanningContent(); + presenter.attachView(this); + faActivity = (ZimManageActivity) super.getActivity(); + llLayout = (LinearLayout) inflater.inflate(R.layout.activity_library, container, false); + swipeRefreshLayout.setOnRefreshListener(() -> refreshFragment()); libraryAdapter = new LibraryAdapter(super.getContext()); libraryList.setAdapter(libraryAdapter); - presenter.attachView(this); DownloadService.setDownloadFragment(faActivity.mSectionsPagerAdapter.getDownloadFragment()); - NetworkInfo network = conMan.getActiveNetworkInfo(); - if (network == null || !network.isConnected()) { - noNetworkConnection(); - } - networkBroadcastReceiver = new NetworkBroadcastReceiver(); faActivity.registerReceiver(networkBroadcastReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); isReceiverRegistered = true; presenter.loadRunningDownloadsFromDb(getActivity()); - // The FragmentActivity doesn't contain the layout directly so we must use our instance of LinearLayout : - // llLayout.findViewById(R.id.someGuiElement); - // Instead of : - // findViewById(R.id.someGuiElement); - return llLayout; // We must return the loaded Layout + return llLayout; } @@ -177,17 +157,26 @@ public class LibraryFragment extends Fragment networkText.setText(R.string.no_network_msg); networkText.setVisibility(View.VISIBLE); - permissionButton.setVisibility(View.GONE); + permissionButton.setVisibility(GONE); swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setEnabled(false); + libraryList.setVisibility(View.INVISIBLE); + TestingUtils.unbindResource(LibraryFragment.class); + } + + @Override + public void displayNoItemsFound() { + networkText.setText(R.string.no_items_msg); + networkText.setVisibility(View.VISIBLE); + permissionButton.setVisibility(GONE); TestingUtils.unbindResource(LibraryFragment.class); } @Override public void displayScanningContent() { if (!swipeRefreshLayout.isRefreshing()) { - networkText.setVisibility(View.GONE); - permissionButton.setVisibility(View.GONE); + networkText.setVisibility(GONE); + permissionButton.setVisibility(GONE); swipeRefreshLayout.setEnabled(true); swipeRefreshLayout.setRefreshing(true); TestingUtils.bindResource(LibraryFragment.class); @@ -197,16 +186,12 @@ public class LibraryFragment extends Fragment @Override public void stopScanningContent() { - networkText.setVisibility(View.GONE); - permissionButton.setVisibility(View.GONE); + networkText.setVisibility(GONE); + permissionButton.setVisibility(GONE); swipeRefreshLayout.setRefreshing(false); TestingUtils.unbindResource(LibraryFragment.class); } - public void noNetworkConnection() { - displayNoNetworkConnection(); - } - public void refreshFragment() { NetworkInfo network = conMan.getActiveNetworkInfo(); if (network == null || !network.isConnected()) { @@ -214,14 +199,13 @@ public class LibraryFragment extends Fragment swipeRefreshLayout.setRefreshing(false); return; } - networkBroadcastReceiver.onReceive(super.getActivity(), null); } @Override public void onDestroyView() { super.onDestroyView(); - if (mBound) { + if (mBound && super.getActivity() != null) { super.getActivity().unbindService(mConnection.downloadServiceInterface); mBound = false; } @@ -317,7 +301,6 @@ public class LibraryFragment extends Fragment editor.apply(); } - public class DownloadServiceConnection { public DownloadServiceInterface downloadServiceInterface; @@ -345,11 +328,17 @@ public class LibraryFragment extends Fragment public void onReceive(Context context, Intent intent) { NetworkInfo network = conMan.getActiveNetworkInfo(); + if (network == null || !network.isConnected()) { + displayNoNetworkConnection(); + } + if ((books == null || books.isEmpty()) && network != null && network.isConnected()) { presenter.loadBooks(); - permissionButton.setVisibility(View.GONE); - networkText.setVisibility(View.GONE); + permissionButton.setVisibility(GONE); + networkText.setVisibility(GONE); + libraryList.setVisibility(View.VISIBLE); } + } } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryPresenter.java b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryPresenter.java index 5db26d0e5..151775290 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryPresenter.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryPresenter.java @@ -36,7 +36,7 @@ public class LibraryPresenter extends BasePresenter { }, error -> { String msg = error.getLocalizedMessage(); Log.w("kiwixLibrary", "Error loading books:" + (msg != null ? msg : "(null)")); - getMvpView().displayNoNetworkConnection(); + getMvpView().displayNoItemsFound(); }); } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryViewCallback.java b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryViewCallback.java index 747157481..324fd3810 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryViewCallback.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryViewCallback.java @@ -16,6 +16,8 @@ public interface LibraryViewCallback extends ViewCallback { void displayNoNetworkConnection(); + void displayNoItemsFound(); + void displayScanningContent(); void stopScanningContent(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e97230f84..d1997d041 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -189,4 +189,5 @@ Do not ask anymore Selected languages: Other languages: + No items available From 02dbea72d795e3948bbf82972df59e6c24625648 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 15 Dec 2017 11:49:51 +0000 Subject: [PATCH 2/5] stop refreshing when no items exist --- app/src/main/AndroidManifest.xml | 1 - .../kiwixmobile/zim_manager/library_view/LibraryFragment.java | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 70268b8b0..406ced277 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -30,7 +30,6 @@ android:label="@string/app_name"> - diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java index cd1b39f0f..16b1c092b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java @@ -114,10 +114,11 @@ public class LibraryFragment extends Fragment setupDagger(); TestingUtils.bindResource(LibraryFragment.class); + llLayout = (LinearLayout) inflater.inflate(R.layout.activity_library, container, false); ButterKnife.bind(this, llLayout); presenter.attachView(this); + faActivity = (ZimManageActivity) super.getActivity(); - llLayout = (LinearLayout) inflater.inflate(R.layout.activity_library, container, false); swipeRefreshLayout.setOnRefreshListener(() -> refreshFragment()); libraryAdapter = new LibraryAdapter(super.getContext()); libraryList.setAdapter(libraryAdapter); @@ -169,6 +170,7 @@ public class LibraryFragment extends Fragment networkText.setText(R.string.no_items_msg); networkText.setVisibility(View.VISIBLE); permissionButton.setVisibility(GONE); + swipeRefreshLayout.setRefreshing(false); TestingUtils.unbindResource(LibraryFragment.class); } From edacdfd13be516cd4bd87acd83bc557a184172d8 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 15 Dec 2017 15:19:39 +0000 Subject: [PATCH 3/5] update ui ("No downloads") when download stopped from notification --- .../downloader/DownloadFragment.java | 503 ++++++++++-------- 1 file changed, 271 insertions(+), 232 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java index 0a75d3440..14f8ee2fd 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java @@ -5,6 +5,7 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.Application; import android.content.Context; +import android.database.DataSetObserver; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; @@ -26,6 +27,7 @@ import android.widget.RelativeLayout; import android.widget.TextView; import java.util.Locale; + import org.kiwix.kiwixmobile.KiwixApplication; import org.kiwix.kiwixmobile.KiwixMobileActivity; import org.kiwix.kiwixmobile.R; @@ -45,259 +47,296 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; public class DownloadFragment extends Fragment { - public static LinkedHashMap mDownloads = new LinkedHashMap<>(); - public static LinkedHashMap mDownloadFiles = new LinkedHashMap<>(); - public RelativeLayout relLayout; - public ListView listView; - public static DownloadAdapter downloadAdapter; - private ZimManageActivity zimManageActivity; - CoordinatorLayout mainLayout; - private Activity faActivity; - private boolean hasArtificiallyPaused; + public static LinkedHashMap mDownloads = new LinkedHashMap<>(); + public static LinkedHashMap mDownloadFiles = new LinkedHashMap<>(); + public RelativeLayout relLayout; + public ListView listView; + public static DownloadAdapter downloadAdapter; + private ZimManageActivity zimManageActivity; + CoordinatorLayout mainLayout; + private Activity faActivity; + private boolean hasArtificiallyPaused; - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - faActivity = super.getActivity(); - relLayout = (RelativeLayout) inflater.inflate(R.layout.download_management, container, false); + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + faActivity = super.getActivity(); + relLayout = (RelativeLayout) inflater.inflate(R.layout.download_management, container, false); - zimManageActivity = (ZimManageActivity) super.getActivity(); - listView = (ListView) relLayout.findViewById(R.id.zim_downloader_list); - downloadAdapter = new DownloadAdapter(mDownloads); - listView.setAdapter(downloadAdapter); - mainLayout = (CoordinatorLayout) faActivity.findViewById(R.id.zim_manager_main_activity); - return relLayout; - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - updateNoDownloads(); - } - - private void updateNoDownloads() { - if (faActivity == null) { - return; - } - TextView noDownloadsText = (TextView) faActivity.findViewById(R.id.download_management_no_downloads); - if (noDownloadsText == null) return; - if (listView.getCount() == 0) { - noDownloadsText.setVisibility(View.VISIBLE); - } else if (listView.getCount() > 0) { - noDownloadsText.setVisibility(View.GONE); - } - } - - 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 static String toHumanReadableTime(int seconds) { - final double MINUTES = 60; - final double HOURS = 60 * MINUTES; - final double DAYS = 24 * HOURS; - - if (Math.round(seconds / DAYS) > 0) - return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / DAYS), - KiwixApplication.getInstance().getResources().getString(R.string.time_day), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - if (Math.round(seconds / HOURS) > 0) - return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / HOURS), - KiwixApplication.getInstance().getResources().getString(R.string.time_hour), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - if (Math.round(seconds / MINUTES) > 0) - return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / MINUTES), - KiwixApplication.getInstance().getResources().getString(R.string.time_minute), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - return String.format(Locale.getDefault(), "%d %s %s", seconds, - KiwixApplication.getInstance().getResources().getString(R.string.time_second), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - } - - public class DownloadAdapter extends BaseAdapter { - - private LinkedHashMap mData = new LinkedHashMap<>(); - private Integer[] mKeys; - - public DownloadAdapter(LinkedHashMap data) { - mData = data; - mKeys = mData.keySet().toArray(new Integer[data.size()]); + zimManageActivity = (ZimManageActivity) super.getActivity(); + listView = (ListView) relLayout.findViewById(R.id.zim_downloader_list); + downloadAdapter = new DownloadAdapter(mDownloads); + downloadAdapter.registerDataSetObserver(this); + listView.setAdapter(downloadAdapter); + mainLayout = (CoordinatorLayout) faActivity.findViewById(R.id.zim_manager_main_activity); + return relLayout; } @Override - public int getCount() { - return mData.size(); + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + updateNoDownloads(); } - @Override - public LibraryNetworkEntity.Book getItem(int position) { - return mData.get(mKeys[position]); - } - - @Override - public long getItemId(int arg0) { - return arg0; - } - - public void complete(int notificationID) { - if (!isAdded()) { - return; - } - int position = Arrays.asList(mKeys).indexOf(notificationID); - ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); - if (viewGroup == null) { - mDownloads.remove(mKeys[position]); - mDownloadFiles.remove(mKeys[position]); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - } - ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause); - pause.setEnabled(false); - String fileName = FileUtils.getFileName(mDownloadFiles.get(mKeys[position])); - { - Snackbar completeSnack = Snackbar.make(mainLayout, getResources().getString(R.string.download_complete_snackbar), Snackbar.LENGTH_LONG); - completeSnack.setAction(getResources().getString(R.string.open), v -> ZimFileSelectFragment.finishResult(fileName)).setActionTextColor(getResources().getColor(R.color.white)).show(); - } - ZimFileSelectFragment zimFileSelectFragment = (ZimFileSelectFragment) zimManageActivity.mSectionsPagerAdapter.getItem(0); - zimFileSelectFragment.addBook(fileName); - mDownloads.remove(mKeys[position]); - mDownloadFiles.remove(mKeys[position]); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - } - - public void updateProgress(int progress, int notificationID) { - if (isAdded()) { - int position = Arrays.asList(mKeys).indexOf(notificationID); - ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); - if (viewGroup == null) { - return; + private void updateNoDownloads() { + if (faActivity == null) { + return; } - ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress); - downloadProgress.setProgress(progress); - TextView timeRemaining = (TextView) viewGroup.findViewById(R.id.time_remaining); - int secLeft = LibraryFragment.mService.timeRemaining.get(mKeys[position], -1); - if (secLeft != -1) - timeRemaining.setText(toHumanReadableTime(secLeft)); - } - } - - private void setPlayState(ImageView pauseButton, int position, int newPlayState) { - if(newPlayState == DownloadService.PLAY) { //Playing - if (LibraryFragment.mService.playDownload(mKeys[position])) - pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); - } else { //Pausing - LibraryFragment.mService.pauseDownload(mKeys[position]); - pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); + TextView noDownloadsText = (TextView) faActivity.findViewById(R.id.download_management_no_downloads); + if (noDownloadsText == null) return; + if (listView.getCount() == 0) { + noDownloadsText.setVisibility(View.VISIBLE); + } else if (listView.getCount() > 0) { + noDownloadsText.setVisibility(View.GONE); } } @Override - public View getView(int position, View convertView, ViewGroup parent) { - // Get the data item for this position - // Check if an existing view is being reused, otherwise inflate the view - if (convertView == null) { - convertView = LayoutInflater.from(faActivity).inflate(R.layout.download_item, parent, false); - } - mKeys = mData.keySet().toArray(new Integer[mData.size()]); - // Lookup view for data population - //downloadProgress.setProgress(download.progress); - // Populate the data into the template view using the data object - TextView title = (TextView) convertView.findViewById(R.id.title); - TextView description = (TextView) convertView.findViewById(R.id.description); - TextView timeRemaining = (TextView) convertView.findViewById(R.id.time_remaining); - ImageView imageView = (ImageView) convertView.findViewById(R.id.favicon); - title.setText(getItem(position).getTitle()); - description.setText(getItem(position).getDescription()); - imageView.setImageBitmap(StringToBitMap(getItem(position).getFavicon())); + public void onDestroy() { + super.onDestroy(); + downloadAdapter.unRegisterDataSetObserver(); + } - ProgressBar downloadProgress = (ProgressBar) convertView.findViewById(R.id.downloadProgress); - ImageView pause = (ImageView) convertView.findViewById(R.id.pause); - - if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == 0) { - downloadProgress.setProgress(0); - pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); - } else { - downloadProgress.setProgress(LibraryFragment.mService.downloadProgress.get(mKeys[position])); - if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PAUSE) { - pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); - } - if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY) { - pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); - } - } - - pause.setOnClickListener(v -> { - 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; - } - - timeRemaining.setText(""); - - setPlayState(pause, position, newPlayPauseState); - }); - - - ImageView stop = (ImageView) convertView.findViewById(R.id.stop); - stop.setOnClickListener(v -> { - hasArtificiallyPaused = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY; - setPlayState(pause, position, DownloadService.PAUSE); - new AlertDialog.Builder(faActivity, dialogStyle()) - .setTitle(R.string.confirm_stop_download_title) - .setMessage(R.string.confirm_stop_download_msg) + 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) -> { - LibraryFragment.mService.stopDownload(mKeys[position]); - mDownloads.remove(mKeys[position]); - mDownloadFiles.remove(mKeys[position]); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) { - zimManageActivity .mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery()); - } + PreferenceManager.getDefaultSharedPreferences(context) + .edit() + .putBoolean(KiwixSettingsActivity.PREF_WIFI_ONLY, false) + .apply(); + KiwixMobileActivity.wifiOnly = false; + yesAction.run(); }) .setNegativeButton(R.string.no, (dialog, i) -> { - if(hasArtificiallyPaused) { - hasArtificiallyPaused = false; - setPlayState(pause, position, DownloadService.PLAY); - } }) .show(); - }); - - // Return the completed view to render on screen - return convertView; } - } - public void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) { - mDownloads.put(position, book); - mDownloadFiles.put(position, fileName); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - } + public static String toHumanReadableTime(int seconds) { + final double MINUTES = 60; + final double HOURS = 60 * MINUTES; + final double DAYS = 24 * HOURS; - public Bitmap StringToBitMap(String encodedString) { - try { - byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT); - return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); - } catch (Exception e) { - e.getMessage(); - return null; + if (Math.round(seconds / DAYS) > 0) + return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / DAYS), + KiwixApplication.getInstance().getResources().getString(R.string.time_day), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + if (Math.round(seconds / HOURS) > 0) + return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / HOURS), + KiwixApplication.getInstance().getResources().getString(R.string.time_hour), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + if (Math.round(seconds / MINUTES) > 0) + return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / MINUTES), + KiwixApplication.getInstance().getResources().getString(R.string.time_minute), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + return String.format(Locale.getDefault(), "%d %s %s", seconds, + KiwixApplication.getInstance().getResources().getString(R.string.time_second), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + } + + public class DownloadAdapter extends BaseAdapter { + + private LinkedHashMap mData = new LinkedHashMap<>(); + private Integer[] mKeys; + private DataSetObserver dataSetObserver; + + public DownloadAdapter(LinkedHashMap data) { + mData = data; + mKeys = mData.keySet().toArray(new Integer[data.size()]); + } + + @Override + public int getCount() { + return mData.size(); + } + + @Override + public LibraryNetworkEntity.Book getItem(int position) { + return mData.get(mKeys[position]); + } + + @Override + public long getItemId(int arg0) { + return arg0; + } + + public void complete(int notificationID) { + if (!isAdded()) { + return; + } + int position = Arrays.asList(mKeys).indexOf(notificationID); + ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); + if (viewGroup == null) { + mDownloads.remove(mKeys[position]); + mDownloadFiles.remove(mKeys[position]); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); + } + ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause); + pause.setEnabled(false); + String fileName = FileUtils.getFileName(mDownloadFiles.get(mKeys[position])); + { + Snackbar completeSnack = Snackbar.make(mainLayout, getResources().getString(R.string.download_complete_snackbar), Snackbar.LENGTH_LONG); + completeSnack.setAction(getResources().getString(R.string.open), v -> ZimFileSelectFragment.finishResult(fileName)).setActionTextColor(getResources().getColor(R.color.white)).show(); + } + ZimFileSelectFragment zimFileSelectFragment = (ZimFileSelectFragment) zimManageActivity.mSectionsPagerAdapter.getItem(0); + zimFileSelectFragment.addBook(fileName); + mDownloads.remove(mKeys[position]); + mDownloadFiles.remove(mKeys[position]); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); + } + + public void updateProgress(int progress, int notificationID) { + if (isAdded()) { + int position = Arrays.asList(mKeys).indexOf(notificationID); + ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); + if (viewGroup == null) { + return; + } + ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress); + downloadProgress.setProgress(progress); + TextView timeRemaining = (TextView) viewGroup.findViewById(R.id.time_remaining); + int secLeft = LibraryFragment.mService.timeRemaining.get(mKeys[position], -1); + if (secLeft != -1) + timeRemaining.setText(toHumanReadableTime(secLeft)); + } + } + + private void setPlayState(ImageView pauseButton, int position, int newPlayState) { + if (newPlayState == DownloadService.PLAY) { //Playing + if (LibraryFragment.mService.playDownload(mKeys[position])) + pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); + } else { //Pausing + LibraryFragment.mService.pauseDownload(mKeys[position]); + pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); + } + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + // Get the data item for this position + // Check if an existing view is being reused, otherwise inflate the view + if (convertView == null) { + convertView = LayoutInflater.from(faActivity).inflate(R.layout.download_item, parent, false); + } + mKeys = mData.keySet().toArray(new Integer[mData.size()]); + // Lookup view for data population + //downloadProgress.setProgress(download.progress); + // Populate the data into the template view using the data object + TextView title = (TextView) convertView.findViewById(R.id.title); + TextView description = (TextView) convertView.findViewById(R.id.description); + TextView timeRemaining = (TextView) convertView.findViewById(R.id.time_remaining); + ImageView imageView = (ImageView) convertView.findViewById(R.id.favicon); + title.setText(getItem(position).getTitle()); + description.setText(getItem(position).getDescription()); + imageView.setImageBitmap(StringToBitMap(getItem(position).getFavicon())); + + ProgressBar downloadProgress = (ProgressBar) convertView.findViewById(R.id.downloadProgress); + ImageView pause = (ImageView) convertView.findViewById(R.id.pause); + + if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == 0) { + downloadProgress.setProgress(0); + pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); + } else { + downloadProgress.setProgress(LibraryFragment.mService.downloadProgress.get(mKeys[position])); + if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PAUSE) { + pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); + } + if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY) { + pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); + } + } + + pause.setOnClickListener(v -> { + 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; + } + + timeRemaining.setText(""); + + setPlayState(pause, position, newPlayPauseState); + }); + + + ImageView stop = (ImageView) convertView.findViewById(R.id.stop); + stop.setOnClickListener(v -> { + hasArtificiallyPaused = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY; + setPlayState(pause, position, DownloadService.PAUSE); + new AlertDialog.Builder(faActivity, dialogStyle()) + .setTitle(R.string.confirm_stop_download_title) + .setMessage(R.string.confirm_stop_download_msg) + .setPositiveButton(R.string.yes, (dialog, i) -> { + LibraryFragment.mService.stopDownload(mKeys[position]); + mDownloads.remove(mKeys[position]); + mDownloadFiles.remove(mKeys[position]); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); + if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) { + zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery()); + } + }) + .setNegativeButton(R.string.no, (dialog, i) -> { + if (hasArtificiallyPaused) { + hasArtificiallyPaused = false; + setPlayState(pause, position, DownloadService.PLAY); + } + }) + .show(); + }); + + // Return the completed view to render on screen + return convertView; + } + + public void registerDataSetObserver(DownloadFragment downloadFragment) { + if (dataSetObserver == null) { + dataSetObserver = new DataSetObserver() { + @Override + public void onChanged() { + super.onChanged(); + downloadFragment.updateNoDownloads(); + } + + @Override + public void onInvalidated() { + super.onInvalidated(); + downloadFragment.updateNoDownloads(); + } + }; + + registerDataSetObserver(dataSetObserver); + } + } + + public void unRegisterDataSetObserver() { + if (dataSetObserver != null) { + unregisterDataSetObserver(dataSetObserver); + } + } + } + + public void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) { + mDownloads.put(position, book); + mDownloadFiles.put(position, fileName); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); + } + + public Bitmap StringToBitMap(String encodedString) { + try { + byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT); + return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); + } catch (Exception e) { + e.getMessage(); + return null; + } } - } } From 0e769fe47a6afefc1ebd420d700bd97e9dbc73b3 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 15 Dec 2017 15:27:47 +0000 Subject: [PATCH 4/5] fix indentation --- .../downloader/DownloadFragment.java | 546 +++++++++--------- 1 file changed, 273 insertions(+), 273 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java index 14f8ee2fd..85c661d44 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java @@ -47,296 +47,296 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; public class DownloadFragment extends Fragment { - public static LinkedHashMap mDownloads = new LinkedHashMap<>(); - public static LinkedHashMap mDownloadFiles = new LinkedHashMap<>(); - public RelativeLayout relLayout; - public ListView listView; - public static DownloadAdapter downloadAdapter; - private ZimManageActivity zimManageActivity; - CoordinatorLayout mainLayout; - private Activity faActivity; - private boolean hasArtificiallyPaused; + public static LinkedHashMap mDownloads = new LinkedHashMap<>(); + public static LinkedHashMap mDownloadFiles = new LinkedHashMap<>(); + public RelativeLayout relLayout; + public ListView listView; + public static DownloadAdapter downloadAdapter; + private ZimManageActivity zimManageActivity; + CoordinatorLayout mainLayout; + private Activity faActivity; + private boolean hasArtificiallyPaused; - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - faActivity = super.getActivity(); - relLayout = (RelativeLayout) inflater.inflate(R.layout.download_management, container, false); + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + faActivity = super.getActivity(); + relLayout = (RelativeLayout) inflater.inflate(R.layout.download_management, container, false); - zimManageActivity = (ZimManageActivity) super.getActivity(); - listView = (ListView) relLayout.findViewById(R.id.zim_downloader_list); - downloadAdapter = new DownloadAdapter(mDownloads); - downloadAdapter.registerDataSetObserver(this); - listView.setAdapter(downloadAdapter); - mainLayout = (CoordinatorLayout) faActivity.findViewById(R.id.zim_manager_main_activity); - return relLayout; + zimManageActivity = (ZimManageActivity) super.getActivity(); + listView = (ListView) relLayout.findViewById(R.id.zim_downloader_list); + downloadAdapter = new DownloadAdapter(mDownloads); + downloadAdapter.registerDataSetObserver(this); + listView.setAdapter(downloadAdapter); + mainLayout = (CoordinatorLayout) faActivity.findViewById(R.id.zim_manager_main_activity); + return relLayout; + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + updateNoDownloads(); + } + + private void updateNoDownloads() { + if (faActivity == null) { + return; + } + TextView noDownloadsText = (TextView) faActivity.findViewById(R.id.download_management_no_downloads); + if (noDownloadsText == null) return; + if (listView.getCount() == 0) { + noDownloadsText.setVisibility(View.VISIBLE); + } else if (listView.getCount() > 0) { + noDownloadsText.setVisibility(View.GONE); + } + } + + @Override + public void onDestroy() { + super.onDestroy(); + downloadAdapter.unRegisterDataSetObserver(); + } + + 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 static String toHumanReadableTime(int seconds) { + final double MINUTES = 60; + final double HOURS = 60 * MINUTES; + final double DAYS = 24 * HOURS; + + if (Math.round(seconds / DAYS) > 0) + return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / DAYS), + KiwixApplication.getInstance().getResources().getString(R.string.time_day), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + if (Math.round(seconds / HOURS) > 0) + return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / HOURS), + KiwixApplication.getInstance().getResources().getString(R.string.time_hour), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + if (Math.round(seconds / MINUTES) > 0) + return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / MINUTES), + KiwixApplication.getInstance().getResources().getString(R.string.time_minute), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + return String.format(Locale.getDefault(), "%d %s %s", seconds, + KiwixApplication.getInstance().getResources().getString(R.string.time_second), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + } + + public class DownloadAdapter extends BaseAdapter { + + private LinkedHashMap mData = new LinkedHashMap<>(); + private Integer[] mKeys; + private DataSetObserver dataSetObserver; + + public DownloadAdapter(LinkedHashMap data) { + mData = data; + mKeys = mData.keySet().toArray(new Integer[data.size()]); } @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - updateNoDownloads(); - } - - private void updateNoDownloads() { - if (faActivity == null) { - return; - } - TextView noDownloadsText = (TextView) faActivity.findViewById(R.id.download_management_no_downloads); - if (noDownloadsText == null) return; - if (listView.getCount() == 0) { - noDownloadsText.setVisibility(View.VISIBLE); - } else if (listView.getCount() > 0) { - noDownloadsText.setVisibility(View.GONE); - } + public int getCount() { + return mData.size(); } @Override - public void onDestroy() { - super.onDestroy(); - downloadAdapter.unRegisterDataSetObserver(); + public LibraryNetworkEntity.Book getItem(int position) { + return mData.get(mKeys[position]); } - 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(); + @Override + public long getItemId(int arg0) { + return arg0; } - public static String toHumanReadableTime(int seconds) { - final double MINUTES = 60; - final double HOURS = 60 * MINUTES; - final double DAYS = 24 * HOURS; - - if (Math.round(seconds / DAYS) > 0) - return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / DAYS), - KiwixApplication.getInstance().getResources().getString(R.string.time_day), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - if (Math.round(seconds / HOURS) > 0) - return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / HOURS), - KiwixApplication.getInstance().getResources().getString(R.string.time_hour), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - if (Math.round(seconds / MINUTES) > 0) - return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / MINUTES), - KiwixApplication.getInstance().getResources().getString(R.string.time_minute), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - return String.format(Locale.getDefault(), "%d %s %s", seconds, - KiwixApplication.getInstance().getResources().getString(R.string.time_second), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); - } - - public class DownloadAdapter extends BaseAdapter { - - private LinkedHashMap mData = new LinkedHashMap<>(); - private Integer[] mKeys; - private DataSetObserver dataSetObserver; - - public DownloadAdapter(LinkedHashMap data) { - mData = data; - mKeys = mData.keySet().toArray(new Integer[data.size()]); - } - - @Override - public int getCount() { - return mData.size(); - } - - @Override - public LibraryNetworkEntity.Book getItem(int position) { - return mData.get(mKeys[position]); - } - - @Override - public long getItemId(int arg0) { - return arg0; - } - - public void complete(int notificationID) { - if (!isAdded()) { - return; - } - int position = Arrays.asList(mKeys).indexOf(notificationID); - ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); - if (viewGroup == null) { - mDownloads.remove(mKeys[position]); - mDownloadFiles.remove(mKeys[position]); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - } - ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause); - pause.setEnabled(false); - String fileName = FileUtils.getFileName(mDownloadFiles.get(mKeys[position])); - { - Snackbar completeSnack = Snackbar.make(mainLayout, getResources().getString(R.string.download_complete_snackbar), Snackbar.LENGTH_LONG); - completeSnack.setAction(getResources().getString(R.string.open), v -> ZimFileSelectFragment.finishResult(fileName)).setActionTextColor(getResources().getColor(R.color.white)).show(); - } - ZimFileSelectFragment zimFileSelectFragment = (ZimFileSelectFragment) zimManageActivity.mSectionsPagerAdapter.getItem(0); - zimFileSelectFragment.addBook(fileName); - mDownloads.remove(mKeys[position]); - mDownloadFiles.remove(mKeys[position]); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - } - - public void updateProgress(int progress, int notificationID) { - if (isAdded()) { - int position = Arrays.asList(mKeys).indexOf(notificationID); - ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); - if (viewGroup == null) { - return; - } - ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress); - downloadProgress.setProgress(progress); - TextView timeRemaining = (TextView) viewGroup.findViewById(R.id.time_remaining); - int secLeft = LibraryFragment.mService.timeRemaining.get(mKeys[position], -1); - if (secLeft != -1) - timeRemaining.setText(toHumanReadableTime(secLeft)); - } - } - - private void setPlayState(ImageView pauseButton, int position, int newPlayState) { - if (newPlayState == DownloadService.PLAY) { //Playing - if (LibraryFragment.mService.playDownload(mKeys[position])) - pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); - } else { //Pausing - LibraryFragment.mService.pauseDownload(mKeys[position]); - pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); - } - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - // Get the data item for this position - // Check if an existing view is being reused, otherwise inflate the view - if (convertView == null) { - convertView = LayoutInflater.from(faActivity).inflate(R.layout.download_item, parent, false); - } - mKeys = mData.keySet().toArray(new Integer[mData.size()]); - // Lookup view for data population - //downloadProgress.setProgress(download.progress); - // Populate the data into the template view using the data object - TextView title = (TextView) convertView.findViewById(R.id.title); - TextView description = (TextView) convertView.findViewById(R.id.description); - TextView timeRemaining = (TextView) convertView.findViewById(R.id.time_remaining); - ImageView imageView = (ImageView) convertView.findViewById(R.id.favicon); - title.setText(getItem(position).getTitle()); - description.setText(getItem(position).getDescription()); - imageView.setImageBitmap(StringToBitMap(getItem(position).getFavicon())); - - ProgressBar downloadProgress = (ProgressBar) convertView.findViewById(R.id.downloadProgress); - ImageView pause = (ImageView) convertView.findViewById(R.id.pause); - - if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == 0) { - downloadProgress.setProgress(0); - pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); - } else { - downloadProgress.setProgress(LibraryFragment.mService.downloadProgress.get(mKeys[position])); - if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PAUSE) { - pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); - } - if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY) { - pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); - } - } - - pause.setOnClickListener(v -> { - 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; - } - - timeRemaining.setText(""); - - setPlayState(pause, position, newPlayPauseState); - }); - - - ImageView stop = (ImageView) convertView.findViewById(R.id.stop); - stop.setOnClickListener(v -> { - hasArtificiallyPaused = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY; - setPlayState(pause, position, DownloadService.PAUSE); - new AlertDialog.Builder(faActivity, dialogStyle()) - .setTitle(R.string.confirm_stop_download_title) - .setMessage(R.string.confirm_stop_download_msg) - .setPositiveButton(R.string.yes, (dialog, i) -> { - LibraryFragment.mService.stopDownload(mKeys[position]); - mDownloads.remove(mKeys[position]); - mDownloadFiles.remove(mKeys[position]); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) { - zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery()); - } - }) - .setNegativeButton(R.string.no, (dialog, i) -> { - if (hasArtificiallyPaused) { - hasArtificiallyPaused = false; - setPlayState(pause, position, DownloadService.PLAY); - } - }) - .show(); - }); - - // Return the completed view to render on screen - return convertView; - } - - public void registerDataSetObserver(DownloadFragment downloadFragment) { - if (dataSetObserver == null) { - dataSetObserver = new DataSetObserver() { - @Override - public void onChanged() { - super.onChanged(); - downloadFragment.updateNoDownloads(); - } - - @Override - public void onInvalidated() { - super.onInvalidated(); - downloadFragment.updateNoDownloads(); - } - }; - - registerDataSetObserver(dataSetObserver); - } - } - - public void unRegisterDataSetObserver() { - if (dataSetObserver != null) { - unregisterDataSetObserver(dataSetObserver); - } - } - } - - public void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) { - mDownloads.put(position, book); - mDownloadFiles.put(position, fileName); + public void complete(int notificationID) { + if (!isAdded()) { + return; + } + int position = Arrays.asList(mKeys).indexOf(notificationID); + ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); + if (viewGroup == null) { + mDownloads.remove(mKeys[position]); + mDownloadFiles.remove(mKeys[position]); downloadAdapter.notifyDataSetChanged(); updateNoDownloads(); + } + ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause); + pause.setEnabled(false); + String fileName = FileUtils.getFileName(mDownloadFiles.get(mKeys[position])); + { + Snackbar completeSnack = Snackbar.make(mainLayout, getResources().getString(R.string.download_complete_snackbar), Snackbar.LENGTH_LONG); + completeSnack.setAction(getResources().getString(R.string.open), v -> ZimFileSelectFragment.finishResult(fileName)).setActionTextColor(getResources().getColor(R.color.white)).show(); + } + ZimFileSelectFragment zimFileSelectFragment = (ZimFileSelectFragment) zimManageActivity.mSectionsPagerAdapter.getItem(0); + zimFileSelectFragment.addBook(fileName); + mDownloads.remove(mKeys[position]); + mDownloadFiles.remove(mKeys[position]); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); } - public Bitmap StringToBitMap(String encodedString) { - try { - byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT); - return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); - } catch (Exception e) { - e.getMessage(); - return null; + public void updateProgress(int progress, int notificationID) { + if (isAdded()) { + int position = Arrays.asList(mKeys).indexOf(notificationID); + ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); + if (viewGroup == null) { + return; } + ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress); + downloadProgress.setProgress(progress); + TextView timeRemaining = (TextView) viewGroup.findViewById(R.id.time_remaining); + int secLeft = LibraryFragment.mService.timeRemaining.get(mKeys[position], -1); + if (secLeft != -1) + timeRemaining.setText(toHumanReadableTime(secLeft)); + } } + private void setPlayState(ImageView pauseButton, int position, int newPlayState) { + if (newPlayState == DownloadService.PLAY) { //Playing + if (LibraryFragment.mService.playDownload(mKeys[position])) + pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); + } else { //Pausing + LibraryFragment.mService.pauseDownload(mKeys[position]); + pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); + } + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + // Get the data item for this position + // Check if an existing view is being reused, otherwise inflate the view + if (convertView == null) { + convertView = LayoutInflater.from(faActivity).inflate(R.layout.download_item, parent, false); + } + mKeys = mData.keySet().toArray(new Integer[mData.size()]); + // Lookup view for data population + //downloadProgress.setProgress(download.progress); + // Populate the data into the template view using the data object + TextView title = (TextView) convertView.findViewById(R.id.title); + TextView description = (TextView) convertView.findViewById(R.id.description); + TextView timeRemaining = (TextView) convertView.findViewById(R.id.time_remaining); + ImageView imageView = (ImageView) convertView.findViewById(R.id.favicon); + title.setText(getItem(position).getTitle()); + description.setText(getItem(position).getDescription()); + imageView.setImageBitmap(StringToBitMap(getItem(position).getFavicon())); + + ProgressBar downloadProgress = (ProgressBar) convertView.findViewById(R.id.downloadProgress); + ImageView pause = (ImageView) convertView.findViewById(R.id.pause); + + if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == 0) { + downloadProgress.setProgress(0); + pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); + } else { + downloadProgress.setProgress(LibraryFragment.mService.downloadProgress.get(mKeys[position])); + if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PAUSE) { + pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); + } + if (LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY) { + pause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); + } + } + + pause.setOnClickListener(v -> { + 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; + } + + timeRemaining.setText(""); + + setPlayState(pause, position, newPlayPauseState); + }); + + + ImageView stop = (ImageView) convertView.findViewById(R.id.stop); + stop.setOnClickListener(v -> { + hasArtificiallyPaused = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY; + setPlayState(pause, position, DownloadService.PAUSE); + new AlertDialog.Builder(faActivity, dialogStyle()) + .setTitle(R.string.confirm_stop_download_title) + .setMessage(R.string.confirm_stop_download_msg) + .setPositiveButton(R.string.yes, (dialog, i) -> { + LibraryFragment.mService.stopDownload(mKeys[position]); + mDownloads.remove(mKeys[position]); + mDownloadFiles.remove(mKeys[position]); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); + if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) { + zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery()); + } + }) + .setNegativeButton(R.string.no, (dialog, i) -> { + if (hasArtificiallyPaused) { + hasArtificiallyPaused = false; + setPlayState(pause, position, DownloadService.PLAY); + } + }) + .show(); + }); + + // Return the completed view to render on screen + return convertView; + } + + public void registerDataSetObserver(DownloadFragment downloadFragment) { + if (dataSetObserver == null) { + dataSetObserver = new DataSetObserver() { + @Override + public void onChanged() { + super.onChanged(); + downloadFragment.updateNoDownloads(); + } + + @Override + public void onInvalidated() { + super.onInvalidated(); + downloadFragment.updateNoDownloads(); + } + }; + + registerDataSetObserver(dataSetObserver); + } + } + + public void unRegisterDataSetObserver() { + if (dataSetObserver != null) { + unregisterDataSetObserver(dataSetObserver); + } + } + } + + public void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) { + mDownloads.put(position, book); + mDownloadFiles.put(position, fileName); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); + } + + public Bitmap StringToBitMap(String encodedString) { + try { + byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT); + return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); + } catch (Exception e) { + e.getMessage(); + return null; + } + } + } From d51759b4b9a78bce1bada95d5dd8cbc835d7609f Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 15 Dec 2017 15:45:30 +0000 Subject: [PATCH 5/5] more indenting --- .../downloader/DownloadFragment.java | 80 +++++++++---------- .../library_view/LibraryFragment.java | 12 ++- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java index 85c661d44..f847c4f85 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java @@ -98,19 +98,19 @@ 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(); + .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 static String toHumanReadableTime(int seconds) { @@ -120,19 +120,19 @@ public class DownloadFragment extends Fragment { if (Math.round(seconds / DAYS) > 0) return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / DAYS), - KiwixApplication.getInstance().getResources().getString(R.string.time_day), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + KiwixApplication.getInstance().getResources().getString(R.string.time_day), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); if (Math.round(seconds / HOURS) > 0) return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / HOURS), - KiwixApplication.getInstance().getResources().getString(R.string.time_hour), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + KiwixApplication.getInstance().getResources().getString(R.string.time_hour), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); if (Math.round(seconds / MINUTES) > 0) return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / MINUTES), - KiwixApplication.getInstance().getResources().getString(R.string.time_minute), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + KiwixApplication.getInstance().getResources().getString(R.string.time_minute), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); return String.format(Locale.getDefault(), "%d %s %s", seconds, - KiwixApplication.getInstance().getResources().getString(R.string.time_second), - KiwixApplication.getInstance().getResources().getString(R.string.time_left)); + KiwixApplication.getInstance().getResources().getString(R.string.time_second), + KiwixApplication.getInstance().getResources().getString(R.string.time_left)); } public class DownloadAdapter extends BaseAdapter { @@ -270,25 +270,25 @@ public class DownloadFragment extends Fragment { hasArtificiallyPaused = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY; setPlayState(pause, position, DownloadService.PAUSE); new AlertDialog.Builder(faActivity, dialogStyle()) - .setTitle(R.string.confirm_stop_download_title) - .setMessage(R.string.confirm_stop_download_msg) - .setPositiveButton(R.string.yes, (dialog, i) -> { - LibraryFragment.mService.stopDownload(mKeys[position]); - mDownloads.remove(mKeys[position]); - mDownloadFiles.remove(mKeys[position]); - downloadAdapter.notifyDataSetChanged(); - updateNoDownloads(); - if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) { - zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery()); - } - }) - .setNegativeButton(R.string.no, (dialog, i) -> { - if (hasArtificiallyPaused) { - hasArtificiallyPaused = false; - setPlayState(pause, position, DownloadService.PLAY); - } - }) - .show(); + .setTitle(R.string.confirm_stop_download_title) + .setMessage(R.string.confirm_stop_download_msg) + .setPositiveButton(R.string.yes, (dialog, i) -> { + LibraryFragment.mService.stopDownload(mKeys[position]); + mDownloads.remove(mKeys[position]); + mDownloadFiles.remove(mKeys[position]); + downloadAdapter.notifyDataSetChanged(); + updateNoDownloads(); + if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) { + zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery()); + } + }) + .setNegativeButton(R.string.no, (dialog, i) -> { + if (hasArtificiallyPaused) { + hasArtificiallyPaused = false; + setPlayState(pause, position, DownloadService.PLAY); + } + }) + .show(); }); // Return the completed view to render on screen diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java index 2bb00aaa5..6801c7d69 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.java @@ -92,7 +92,8 @@ public class LibraryFragment extends Fragment private DownloadServiceConnection mConnection = new DownloadServiceConnection(); - @Inject ConnectivityManager conMan; + @Inject + ConnectivityManager conMan; private ZimManageActivity faActivity; @@ -145,7 +146,7 @@ public class LibraryFragment extends Fragment faActivity.searchView.getQuery(), i -> stopScanningContent()); } else { - libraryAdapter.getFilter().filter("", i -> stopScanningContent() ); + libraryAdapter.getFilter().filter("", i -> stopScanningContent()); } libraryAdapter.notifyDataSetChanged(); libraryList.setOnItemClickListener(this); @@ -257,7 +258,9 @@ public class LibraryFragment extends Fragment } if (KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getContext())) { - DownloadFragment.showNoWiFiWarning(getContext(), () -> {downloadFile((Book) parent.getAdapter().getItem(position));}); + DownloadFragment.showNoWiFiWarning(getContext(), () -> { + downloadFile((Book) parent.getAdapter().getItem(position)); + }); } else { downloadFile((Book) parent.getAdapter().getItem(position)); } @@ -323,7 +326,8 @@ public class LibraryFragment extends Fragment } @Override - public void onServiceDisconnected(ComponentName arg0) { } + public void onServiceDisconnected(ComponentName arg0) { + } } }