diff --git a/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java b/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java index 8049963..c2a24cf 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java @@ -44,7 +44,6 @@ import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.entities.Instance; import app.fedilab.fedilabtube.client.entities.InstanceParams; import app.fedilab.fedilabtube.drawer.InstanceAdapter; -import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan; import app.fedilab.fedilabtube.viewmodel.InstancesVM; import es.dmoral.toasty.Toasty; @@ -110,134 +109,138 @@ public class InstancePickerActivity extends AppCompatActivity { } }); + if (peertubeInformation != null && peertubeInformation.getLanguages() != null) { + LinkedHashMap languages = new LinkedHashMap<>(peertubeInformation.getLanguages()); + checkedItemsLanguage = new boolean[languages.size()]; + itemsLabelLanguage = new String[languages.size()]; + itemsKeyLanguage = new String[languages.size()]; - LinkedHashMap languages = new LinkedHashMap<>(peertubeInformation.getLanguages()); - checkedItemsLanguage = new boolean[languages.size()]; - itemsLabelLanguage = new String[languages.size()]; - itemsKeyLanguage = new String[languages.size()]; - - pickup_languages.setOnClickListener(v -> { - AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this); + pickup_languages.setOnClickListener(v -> { + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this); - int i = 0; - if (languages.size() > 0) { - Iterator> it = languages.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = it.next(); - itemsLabelLanguage[i] = pair.getValue(); - checkedItemsLanguage[i] = false; - itemsKeyLanguage[i] = pair.getKey(); - it.remove(); - i++; - } - } - - dialogBuilder.setMultiChoiceItems(itemsLabelLanguage, checkedItemsLanguage, (dialog, which, isChecked) -> { - // The user checked or unchecked a box - checkedItemsLanguage[which] = isChecked; - }); - - dialogBuilder.setOnDismissListener(dialogInterface -> { - - SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); - String between = ""; - stringBuilder.append(between); - List langs = new ArrayList<>(); - int j = 0; - for(boolean itemcheked: checkedItemsLanguage){ - if( itemcheked) { - langs.add(itemsKeyLanguage[j]); - String lang = itemsLabelLanguage[j]; - if (lang != null && lang.trim().toLowerCase().compareTo("null") != 0) { - if (between.length() == 0) between = " "; - String tag = " " + lang + " "; - stringBuilder.append(tag); - stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - stringBuilder.append(" "); - } + int i = 0; + if (languages.size() > 0) { + Iterator> it = languages.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + itemsLabelLanguage[i] = pair.getValue(); + checkedItemsLanguage[i] = false; + itemsKeyLanguage[i] = pair.getKey(); + it.remove(); + i++; } - j++; } - instanceParams.setLanguagesOr(langs); - languages_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); - if (languages_view.getText().toString().trim().length() > 0) { - languages_view.setVisibility(View.VISIBLE); - } else { - languages_view.setVisibility(View.GONE); - } - mainLoader.setVisibility(View.VISIBLE); - viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); - }); - dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); - AlertDialog alertDialog = dialogBuilder.create(); - alertDialog.setTitle(getString(R.string.pickup_languages)); - alertDialog.show(); - }); + dialogBuilder.setMultiChoiceItems(itemsLabelLanguage, checkedItemsLanguage, (dialog, which, isChecked) -> { + // The user checked or unchecked a box + checkedItemsLanguage[which] = isChecked; + }); - LinkedHashMap categories = new LinkedHashMap<>(peertubeInformation.getCategories()); - checkedItemsCategory = new boolean[categories.size()]; - itemsLabelCategory = new String[categories.size()]; - itemsKeyCategory = new int[categories.size()]; + dialogBuilder.setOnDismissListener(dialogInterface -> { - - pickup_categories.setOnClickListener(v -> { - AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this); - int i = 0; - if (categories.size() > 0) { - Iterator> it = categories.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = it.next(); - itemsLabelCategory[i] = pair.getValue(); - itemsKeyCategory[i] = pair.getKey(); - checkedItemsCategory[i] = false; - it.remove(); - i++; - } - } - - dialogBuilder.setMultiChoiceItems(itemsLabelCategory, checkedItemsCategory, (dialog, which, isChecked) -> { - // The user checked or unchecked a box - checkedItemsCategory[which] = isChecked; - }); - - dialogBuilder.setOnDismissListener(dialogInterface -> { - int j = 0; - SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); - String between = ""; - stringBuilder.append(between); - List cats = new ArrayList<>(); - for(boolean itemcheked: checkedItemsCategory){ - if( itemcheked) { - cats.add(itemsKeyCategory[j]); - String cat = itemsLabelCategory[j]; - if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) { - if (between.length() == 0) between = " "; - String tag = " " + cat + " "; - stringBuilder.append(tag); - stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - stringBuilder.append(" "); + SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); + String between = ""; + stringBuilder.append(between); + List langs = new ArrayList<>(); + int j = 0; + for (boolean itemcheked : checkedItemsLanguage) { + if (itemcheked) { + langs.add(itemsKeyLanguage[j]); + String lang = itemsLabelLanguage[j]; + if (lang != null && lang.trim().toLowerCase().compareTo("null") != 0) { + if (between.length() == 0) between = " "; + String tag = " " + lang + " "; + stringBuilder.append(tag); + stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + stringBuilder.append(" "); + } } + j++; } - j++; - } - instanceParams.setCategoriesOr(cats); - categories_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); - if (categories_view.getText().toString().trim().length() > 0) { - categories_view.setVisibility(View.VISIBLE); - } else { - categories_view.setVisibility(View.GONE); - } - mainLoader.setVisibility(View.VISIBLE); - viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); - }); - dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); + instanceParams.setLanguagesOr(langs); + languages_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); + if (languages_view.getText().toString().trim().length() > 0) { + languages_view.setVisibility(View.VISIBLE); + } else { + languages_view.setVisibility(View.GONE); + } + mainLoader.setVisibility(View.VISIBLE); + viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); + }); + dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); + + AlertDialog alertDialog = dialogBuilder.create(); + alertDialog.setTitle(getString(R.string.pickup_languages)); + alertDialog.show(); + }); + } + + if (peertubeInformation != null && peertubeInformation.getLanguages() != null) { + LinkedHashMap categories = new LinkedHashMap<>(peertubeInformation.getCategories()); + checkedItemsCategory = new boolean[categories.size()]; + itemsLabelCategory = new String[categories.size()]; + itemsKeyCategory = new int[categories.size()]; + + + pickup_categories.setOnClickListener(v -> { + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this); + int i = 0; + if (categories.size() > 0) { + Iterator> it = categories.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + itemsLabelCategory[i] = pair.getValue(); + itemsKeyCategory[i] = pair.getKey(); + checkedItemsCategory[i] = false; + it.remove(); + i++; + } + } + + dialogBuilder.setMultiChoiceItems(itemsLabelCategory, checkedItemsCategory, (dialog, which, isChecked) -> { + // The user checked or unchecked a box + checkedItemsCategory[which] = isChecked; + }); + + dialogBuilder.setOnDismissListener(dialogInterface -> { + int j = 0; + SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); + String between = ""; + stringBuilder.append(between); + List cats = new ArrayList<>(); + for (boolean itemcheked : checkedItemsCategory) { + if (itemcheked) { + cats.add(itemsKeyCategory[j]); + String cat = itemsLabelCategory[j]; + if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) { + if (between.length() == 0) between = " "; + String tag = " " + cat + " "; + stringBuilder.append(tag); + stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + stringBuilder.append(" "); + } + } + j++; + } + instanceParams.setCategoriesOr(cats); + categories_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); + if (categories_view.getText().toString().trim().length() > 0) { + categories_view.setVisibility(View.VISIBLE); + } else { + categories_view.setVisibility(View.GONE); + } + mainLoader.setVisibility(View.VISIBLE); + viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); + }); + dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); + + AlertDialog alertDialog = dialogBuilder.create(); + alertDialog.setTitle(getString(R.string.pickup_categories)); + alertDialog.show(); + }); + } - AlertDialog alertDialog = dialogBuilder.create(); - alertDialog.setTitle(getString(R.string.pickup_categories)); - alertDialog.show(); - }); mainLoader.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java index 7ee070b..74eac38 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java @@ -80,45 +80,47 @@ public class InstanceAdapter extends RecyclerView.Adapter info_cat = new LinkedHashMap<>(peertubeInformation.getCategories()); - if (instance.getCategories() != null && instance.getCategories().size() > 0 && instance.getSpannableStringBuilder() == null) { - Iterator> it = instance.getCategories().entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = it.next(); - String cat = info_cat.get(pair.getKey()); - stringBuilder.append(between); - if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) { - if (between.length() == 0) between = " "; - String tag = " " + cat + " "; - stringBuilder.append(tag); - stringBuilder.setSpan(new RoundedBackgroundSpan(context), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + if (peertubeInformation != null && peertubeInformation.getCategories() != null) { + LinkedHashMap info_cat = new LinkedHashMap<>(peertubeInformation.getCategories()); + if (instance.getCategories() != null && instance.getCategories().size() > 0 && instance.getSpannableStringBuilder() == null) { + Iterator> it = instance.getCategories().entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + String cat = info_cat.get(pair.getKey()); + stringBuilder.append(between); + if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) { + if (between.length() == 0) between = " "; + String tag = " " + cat + " "; + stringBuilder.append(tag); + stringBuilder.setSpan(new RoundedBackgroundSpan(context), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + } + it.remove(); } - it.remove(); + instance.setSpannableStringBuilder(stringBuilder); } - instance.setSpannableStringBuilder(stringBuilder); } - if( instance.getSpannableStringBuilder() != null) { + if (instance.getSpannableStringBuilder() != null) { holder.tags.setText(instance.getSpannableStringBuilder()); } - LinkedHashMap info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages()); - StringBuilder languages = new StringBuilder(); - if (instance.getLanguages() != null && instance.getLanguages().size() > 0) { - Iterator> it = instance.getLanguages().entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = it.next(); - languages.append(info_lang.get(pair.getValue())).append(" "); - it.remove(); + if (peertubeInformation != null && peertubeInformation.getLanguages() != null) { + LinkedHashMap info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages()); + StringBuilder languages = new StringBuilder(); + if (instance.getLanguages() != null && instance.getLanguages().size() > 0) { + Iterator> it = instance.getLanguages().entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + languages.append(info_lang.get(pair.getValue())).append(" "); + it.remove(); + } + } + if (languages.toString().trim().length() == 0) { + holder.languages.setVisibility(View.GONE); + } else { + holder.languages.setText(languages); + holder.languages.setVisibility(View.VISIBLE); } } - - if (languages.toString().trim().length() == 0) { - holder.languages.setVisibility(View.GONE); - } else { - holder.languages.setText(languages); - holder.languages.setVisibility(View.VISIBLE); - } - if (instance.getDefaultNSFWPolicy().compareTo("do_not_list") != 0) { holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy())); holder.sensitive_content.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/NetworkStateReceiver.java b/app/src/main/java/app/fedilab/fedilabtube/helper/NetworkStateReceiver.java new file mode 100644 index 0000000..e4fe0ef --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/NetworkStateReceiver.java @@ -0,0 +1,84 @@ +package app.fedilab.fedilabtube.helper; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of TubeLab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with TubeLab; if not, + * see . */ + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + +import java.util.HashSet; +import java.util.Set; + + +/** + * Original work from https://stackoverflow.com/a/25873554 + */ +public class NetworkStateReceiver extends BroadcastReceiver { + + protected Set listeners; + protected Boolean connected; + + public NetworkStateReceiver() { + listeners = new HashSet<>(); + connected = null; + } + + @Override + public void onReceive(Context context, Intent intent) { + if (intent == null || intent.getExtras() == null) + return; + ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + assert manager != null; + NetworkInfo ni = manager.getActiveNetworkInfo(); + if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) { + connected = true; + } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { + connected = false; + } + notifyStateToAll(); + } + + + private void notifyStateToAll() { + for (NetworkStateReceiverListener listener : listeners) + notifyState(listener); + } + + private void notifyState(NetworkStateReceiverListener listener) { + if (connected == null || listener == null) + return; + if (connected) + listener.networkAvailable(); + else + listener.networkUnavailable(); + } + + public void addListener(NetworkStateReceiverListener l) { + listeners.add(l); + notifyState(l); + } + + public void removeListener(NetworkStateReceiverListener l) { + listeners.remove(l); + } + + public interface NetworkStateReceiverListener { + void networkAvailable(); + + void networkUnavailable(); + } +} \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java b/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java index 65c9872..10cba08 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java +++ b/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java @@ -17,6 +17,7 @@ package app.fedilab.fedilabtube.services; import android.app.Notification; import android.app.Service; import android.content.Intent; +import android.content.IntentFilter; import android.os.Build; import android.os.IBinder; @@ -29,13 +30,25 @@ import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.PeertubeInformation; +import app.fedilab.fedilabtube.helper.NetworkStateReceiver; import static app.fedilab.fedilabtube.MainActivity.peertubeInformation; -public class RetrieveInfoService extends Service { +public class RetrieveInfoService extends Service implements NetworkStateReceiver.NetworkStateReceiverListener { static String NOTIFICATION_CHANNEL_ID = "update_info"; + private NetworkStateReceiver networkStateReceiver; + + + public void onCreate() { + super.onCreate(); + networkStateReceiver = new NetworkStateReceiver(); + networkStateReceiver.addListener(this); + registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); + + + } @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -92,4 +105,44 @@ public class RetrieveInfoService extends Service { public IBinder onBind(Intent intent) { return null; } + + + @Override + public void onDestroy() { + super.onDestroy(); + if (networkStateReceiver != null) { + networkStateReceiver.removeListener(this); + unregisterReceiver(networkStateReceiver); + } + } + + @Override + public void networkAvailable() { + Thread thread = new Thread() { + + @Override + public void run() { + peertubeInformation = new PeertubeInformation(); + peertubeInformation.setCategories(new LinkedHashMap<>()); + peertubeInformation.setLanguages(new LinkedHashMap<>()); + peertubeInformation.setLicences(new LinkedHashMap<>()); + peertubeInformation.setPrivacies(new LinkedHashMap<>()); + peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>()); + peertubeInformation.setTranslations(new LinkedHashMap<>()); + try { + peertubeInformation = new PeertubeAPI(RetrieveInfoService.this).getPeertubeInformation(); + } catch (HttpsConnection.HttpsConnectionException e) { + e.printStackTrace(); + } + stopForeground(true); + + } + }; + thread.start(); + } + + @Override + public void networkUnavailable() { + + } }