Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update only if wifi is available on startup #1 #44

Merged
merged 6 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.LruCache;
import android.view.*;
import android.widget.*;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import de.eknoes.inofficialgolem.updater.GolemFetcher;

import org.jetbrains.annotations.NotNull;

import java.text.DateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.Callable;

import de.eknoes.inofficialgolem.updater.GolemFetcher;
import de.eknoes.inofficialgolem.utils.NetworkUtils;

/**
* A placeholder fragment containing a simple view.
*/
Expand Down Expand Up @@ -95,7 +106,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view, int position, l
mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
refresh(true);
}
});
}
Expand All @@ -109,19 +120,24 @@ public void onResume() {

if (refresh_limit != 0 && last_refresh + ((long) refresh_limit * 1000 * 60) < new Date().getTime()) {
Log.d(TAG, "onCreate: Refresh, last refresh was " + ((new Date().getTime() - last_refresh) / 1000) + "sec ago");
refresh();
refresh(false);
} else {
Log.d(TAG, "onCreate: No refresh, last refresh was " + (new Date().getTime() - last_refresh) / 1000 + "sec ago");
}
}

void refresh() {
void refresh(boolean force) {
if (!force && PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("only_wifi", false)) {
if (!NetworkUtils.hasWifiConnection(getContext())) {
return;
}
}
if (fetcher == null || fetcher.getStatus() != AsyncTask.Status.RUNNING) {
mSwipeLayout.setRefreshing(true);
fetcher = new GolemFetcher(requireContext(), new Callable<Void>() {
@Override
public Void call() {
if(listAdapter != null) {
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
mSwipeLayout.setRefreshing(false);
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/de/eknoes/inofficialgolem/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package de.eknoes.inofficialgolem;

import static de.eknoes.inofficialgolem.ArticleFragment.FORCE_WEBVIEW;
import static de.eknoes.inofficialgolem.ArticleFragment.NO_ARTICLE;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import static de.eknoes.inofficialgolem.ArticleFragment.*;

public class MainActivity extends AppCompatActivity implements ArticleListFragment.OnArticleSelectedListener {
private static final String TAG = "MainActivity";
private static final String CURRENT_ARTICLE = "currentArticle";
Expand Down Expand Up @@ -131,7 +134,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_refresh) {
((ArticleListFragment) Objects.requireNonNull(getSupportFragmentManager().findFragmentById(R.id.fragment_articlelist))).refresh();
((ArticleListFragment) Objects.requireNonNull(getSupportFragmentManager().findFragmentById(R.id.fragment_articlelist))).refresh(true);
return true;
} else if (id == R.id.action_share) {
//Get Package Link
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.eknoes.inofficialgolem.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {

private NetworkUtils() {
}

private static ConnectivityManager getConnectivityManager(Context context) {
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}

public static boolean hasWifiConnection(Context context) {
ConnectivityManager connectivityManager = getConnectivityManager(context);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifiInfo.isConnected();
}
}
18 changes: 18 additions & 0 deletions app/src/main/res/drawable-night/ic_wlan.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:fillColor="#FFFFFF"
android:pathData="M35.5,15.6c-8.9,1.7 -23.8,9 -30.1,14.8l-4.9,4.3 4.8,5.7c2.7,3.1 5.1,5.6 5.3,5.6 0.3,-0 2.8,-1.8 5.7,-3.9 20.7,-15.5 45.6,-15.9 65.8,-1.2 3.8,2.8 7.1,5.1 7.3,5.1 0.3,-0 2.6,-2.5 5.3,-5.6l4.8,-5.7 -4.9,-4.3c-2.6,-2.4 -8.9,-6.5 -14,-8.9 -14.3,-7.1 -29.2,-9.1 -45.1,-5.9z"
android:strokeColor="#00000000" />
<path
android:fillColor="#FFFFFF"
android:pathData="M38.6,39.6c-6.8,1.6 -14.9,6 -18.8,10.1l-2.8,2.9 5.4,5.3 5.4,5.3 4.2,-3.1c10.9,-7.9 24.8,-7.9 35.8,-0l4.4,3.2 5.5,-5.6 5.6,-5.5 -3.4,-3c-10.4,-9.5 -26.5,-13.2 -41.3,-9.6z"
android:strokeColor="#00000000" />
<path
android:fillColor="#FFFFFF"
android:pathData="M41.5,64c-4.7,2.2 -7.5,4.3 -7.5,5.6 0,0.5 3.6,4.5 8,8.9l8,8 8,-8c4.4,-4.4 7.9,-8.7 7.8,-9.4 -0.2,-0.8 -2.7,-2.8 -5.7,-4.3 -6.4,-3.4 -12.8,-3.6 -18.6,-0.8z"
android:strokeColor="#00000000" />
</vector>
18 changes: 18 additions & 0 deletions app/src/main/res/drawable/ic_wlan.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:fillColor="#000000"
android:pathData="M35.5,15.6c-8.9,1.7 -23.8,9 -30.1,14.8l-4.9,4.3 4.8,5.7c2.7,3.1 5.1,5.6 5.3,5.6 0.3,-0 2.8,-1.8 5.7,-3.9 20.7,-15.5 45.6,-15.9 65.8,-1.2 3.8,2.8 7.1,5.1 7.3,5.1 0.3,-0 2.6,-2.5 5.3,-5.6l4.8,-5.7 -4.9,-4.3c-2.6,-2.4 -8.9,-6.5 -14,-8.9 -14.3,-7.1 -29.2,-9.1 -45.1,-5.9z"
android:strokeColor="#00000000" />
<path
android:fillColor="#000000"
android:pathData="M38.6,39.6c-6.8,1.6 -14.9,6 -18.8,10.1l-2.8,2.9 5.4,5.3 5.4,5.3 4.2,-3.1c10.9,-7.9 24.8,-7.9 35.8,-0l4.4,3.2 5.5,-5.6 5.6,-5.5 -3.4,-3c-10.4,-9.5 -26.5,-13.2 -41.3,-9.6z"
android:strokeColor="#00000000" />
<path
android:fillColor="#000000"
android:pathData="M41.5,64c-4.7,2.2 -7.5,4.3 -7.5,5.6 0,0.5 3.6,4.5 8,8.9l8,8 8,-8c4.4,-4.4 7.9,-8.7 7.8,-9.4 -0.2,-0.8 -2.7,-2.8 -5.7,-4.3 -6.4,-3.4 -12.8,-3.6 -18.6,-0.8z"
android:strokeColor="#00000000" />
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@
<string name="pref_imprint_desc">Zeige Kontaktdaten und rechtliche Informationen von Golem.de</string>
<string name="open_browser">Wählen Sie einen Browser aus</string>
<string name="action_imprint">Golem.de Kontakt</string>
<string name="synchronize_only_if_wi_fi_is_available">Synchronisiere nur mit WLAN</string>
<string name="if_enabled_articles_will_be_synchronized_only_when_wi_fi_is_available">Wenn aktiviert, werden Artikel beim Start nur synchronisiert, wenn WLAN verfügbar ist.</string>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<string name="pref_github_sum">This is not an official golem.de App. If you want to report a bug or have any ideas for improving this application just click here to open our github repository</string>
<string name="action_settings">Settings</string>
<string name="pref_contact_sum">Drop me a mail with feedback at projekte@eknoes.de</string>

<string name="filter_view_golem">Open Golem.de Articles</string>
<string name="licenses">Licenses</string>
<string name="licenses_summary">Show third party licenses</string>
Expand All @@ -74,4 +74,6 @@

<string name="open_browser">Select a browser</string>
<string name="action_imprint">Golem.de Contact</string>
<string name="synchronize_only_if_wi_fi_is_available">Synchronize with Wi-Fi only</string>
<string name="if_enabled_articles_will_be_synchronized_only_when_wi_fi_is_available">If enabled, articles will only be synchronized at startup when Wi-Fi is available.</string>
</resources>
47 changes: 27 additions & 20 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@
app:key="sync_category"
app:title="@string/pref_data_title">
<SeekBarPreference
app:key="article_limit"
app:title="@string/pref_article_limit"
app:summary="@string/pref_article_limit_sum"
app:contentDescription="@string/pref_article_limit_sum"
app:defaultValue="50"
app:min="0"
android:max="200"
app:showSeekBarValue="true"
android:icon="@drawable/ic_history"/>
android:icon="@drawable/ic_history"
android:max="200"
app:contentDescription="@string/pref_article_limit_sum"
app:defaultValue="50"
app:key="article_limit"
app:min="0"
app:showSeekBarValue="true"
app:summary="@string/pref_article_limit_sum"
app:title="@string/pref_article_limit" />

<SeekBarPreference
app:key="refresh_limit"
app:title="@string/pref_article_refresh"
app:summary="@string/pref_article_refresh_sum"
app:contentDescription="@string/pref_article_refresh_sum"
app:defaultValue="5"
app:min="0"
android:max="60"
app:showSeekBarValue="true"
android:icon="@drawable/ic_refresh_black"/>
</PreferenceCategory>
<SeekBarPreference
android:icon="@drawable/ic_refresh_black"
android:max="60"
app:contentDescription="@string/pref_article_refresh_sum"
app:defaultValue="5"
app:key="refresh_limit"
app:min="0"
app:showSeekBarValue="true"
app:summary="@string/pref_article_refresh_sum"
app:title="@string/pref_article_refresh" />

<SwitchPreference
app:defaultValue="false"
app:icon="@drawable/ic_wlan"
app:key="only_wifi"
app:summary="@string/if_enabled_articles_will_be_synchronized_only_when_wi_fi_is_available"
app:title="@string/synchronize_only_if_wi_fi_is_available" />
</PreferenceCategory>
<PreferenceCategory
app:key="accessibility_category"
app:title="@string/pref_access">
Expand Down