Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Improve energy efficiency by applying Dynamic Retry Delay Energy Pattern + Cache Energy Pattern #34

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Refactor4Green - DRD + Cache
  • Loading branch information
anasofiagribeiro committed Jul 23, 2020
commit 7a59d7444d745b2bb82f99eb7f9097479d429ca6
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
android:name=".ReCaptchaActivity"
android:label="@string/recaptcha"/>

<receiver android:name="org.schabi.newpipelegacy.RouterActivity.FetcherService$NetworkStateReceiver"
android:exported="true" android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
Expand Down
87 changes: 75 additions & 12 deletions app/src/main/java/org/schabi/newpipelegacy/RouterActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.schabi.newpipelegacy;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.IntentService;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.app.PendingIntent;
import android.content.*;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
Expand Down Expand Up @@ -626,16 +627,21 @@ public void onCreate() {

@Override
protected void onHandleIntent(@Nullable final Intent intent) {
if (intent == null) {
return;
}
if (checkNetwork()) {
if (intent == null) {
return;
}

final Serializable serializable = intent.getSerializableExtra(KEY_CHOICE);
if (!(serializable instanceof Choice)) {
return;
final Serializable serializable = intent.getSerializableExtra(KEY_CHOICE);
if (!(serializable instanceof Choice)) {
return;
}
Choice playerChoice = (Choice) serializable;
handleChoice(playerChoice);
}
else {
NetworkStateReceiver.enable(getApplicationContext());
}
Choice playerChoice = (Choice) serializable;
handleChoice(playerChoice);
}

public void handleChoice(final Choice choice) {
Expand Down Expand Up @@ -745,6 +751,63 @@ private NotificationCompat.Builder createNotification() {
.setContentText(
getString(R.string.preferred_player_fetcher_notification_message));
}

boolean checkNetwork() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a more descriptive method name and add the access modifier to the method.

final ConnectivityManager connManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
Network activeNetwork = connManager.getActiveNetwork();
if (activeNetwork != null) {
return true;
}
return false;
}

public static class NetworkStateReceiver extends BroadcastReceiver {
private static final String TAG = NetworkStateReceiver.class.getName();

private static FetcherService service;

public static void setService(FetcherService newService) {
service = newService;
}

@Override
public void onReceive(Context context, Intent intent) {
if (service.checkNetwork()) {
NetworkStateReceiver.disable(context);

final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

final Intent innerIntent = new Intent(context, FetcherService.class);
final PendingIntent pendingIntent = PendingIntent.getService(context, 0, innerIntent, 0);

SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
preferences.edit();
boolean autoRefreshEnabled = preferences.getBoolean("pref_auto_refresh_enabled", false);

final String hours = preferences.getString("pref_auto_refresh_enabled", "0");
long hoursLong = Long.parseLong(hours) * 60 * 60 * 1000;

if (autoRefreshEnabled && hoursLong != 0) {
final long alarmTime = preferences.getLong("last_auto_refresh_time", 0) + hoursLong;
alarmManager.set(AlarmManager.RTC, alarmTime, pendingIntent);
} else {
alarmManager.cancel(pendingIntent);
}
}
}

public static void enable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final ComponentName receiver = new ComponentName(context, NetworkStateReceiver.class);
packageManager.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

public static void disable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final ComponentName receiver = new ComponentName(context, NetworkStateReceiver.class);
packageManager.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
}
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.*;

import static org.schabi.newpipelegacy.MainActivity.DEBUG;

Expand All @@ -25,18 +22,28 @@ public class TLSSocketFactoryCompat extends SSLSocketFactory {
private static TLSSocketFactoryCompat instance = null;

private SSLSocketFactory internalSSLSocketFactory;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the added white spaces.

public TLSSocketFactoryCompat() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
SSLSessionContext sslSessionContext = context.getServerSessionContext();
int sessionCacheSize = sslSessionContext.getSessionCacheSize();
if (sessionCacheSize > 0) {
sslSessionContext.setSessionCacheSize(0);
}
internalSSLSocketFactory = context.getSocketFactory();
}


public TLSSocketFactoryCompat(final TrustManager[] tm)
throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tm, new java.security.SecureRandom());
context.init(null, tm, new SecureRandom());
SSLSessionContext sslSessionContext = context.getServerSessionContext();
int sessionCacheSize = sslSessionContext.getSessionCacheSize();
if (sessionCacheSize > 0) {
sslSessionContext.setSessionCacheSize(0);
}
internalSSLSocketFactory = context.getSocketFactory();
}

Expand Down