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

App update notification #1608

Merged
merged 42 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
75a44fb
Added HTTPS request to get version data. Added APK flaor for github a…
krtkush Aug 11, 2018
5e2aa51
Moved the asynctask to its own class.
krtkush Aug 11, 2018
f85e19c
Added notification channel and code to show notification.
krtkush Aug 12, 2018
7c875a8
Merge branch 'dev' of https://github.com/krtkush/NewPipe into 1520_ap…
krtkush Aug 12, 2018
06f20c6
Moved the new version check to the application class.
krtkush Aug 12, 2018
930c971
Added version check in the pop-up player
krtkush Aug 12, 2018
12b93d6
Added new icon for update notification.
krtkush Aug 12, 2018
af42e32
Code refactored and added comments.
krtkush Aug 12, 2018
2a18eac
More refactoring.
krtkush Aug 12, 2018
e7abeb5
Added version code check.
krtkush Aug 16, 2018
04e974b
Bug fix.
krtkush Aug 16, 2018
910c10f
Removed debug code
krtkush Aug 16, 2018
17197ad
Pull request changes begins here.
krtkush Sep 15, 2018
395c958
Conflict resolution.
krtkush Sep 15, 2018
cde5f7d
Merge branch 'dev' of https://github.com/krtkush/NewPipe into 1520_ap…
krtkush Sep 15, 2018
6417bd9
Pull request changes v1.
krtkush Sep 15, 2018
7124d9b
Removed flvor checks. Added update settings under main settings.
krtkush Sep 15, 2018
f1aa3d8
Merge branch 'dev' into 1520_app_update_notif
theScrabi Oct 6, 2018
e234136
Added check for SHA1 key.
krtkush Oct 14, 2018
54ac5e8
Merge branch '1520_app_update_notif' of https://github.com/krtkush/Ne…
krtkush Oct 14, 2018
8ef702f
Removed updates options from settings in case of non github apk.
krtkush Oct 18, 2018
506ffb9
Delete BuildConfig.java
krtkush Oct 18, 2018
2d5bc3a
Delete Manifest.java
krtkush Oct 18, 2018
d1a9033
Delete R.java
krtkush Oct 18, 2018
ec28e97
Delete BuildConfig.java
krtkush Oct 18, 2018
6ef25eb
Delete Manifest.java
krtkush Oct 18, 2018
3c6d27b
Delete R.java
krtkush Oct 18, 2018
fda9b59
Code review changes.
TobiGr Oct 22, 2018
96dac0f
Code review suggested changes.
krtkush Oct 22, 2018
c29b064
Merge branch '1520_app_update_notif' of https://github.com/krtkush/Ne…
krtkush Oct 22, 2018
23309e6
Pull request changes.
krtkush Nov 18, 2018
939cc56
Pull request changes v2.
krtkush Nov 18, 2018
ad5535a
Code refactoring, PR changes.
krtkush Nov 19, 2018
26e22f9
Conflict resolution
krtkush Nov 22, 2018
069654c
vector -> png
krtkush Nov 25, 2018
c864b15
Test code revert.
krtkush Nov 25, 2018
7ed460c
Merge branch 'dev' into 1520_app_update_notif
krtkush Dec 2, 2018
f62bfea
Merge branch 'dev' into 1520_app_update_notif
TobiGr Dec 3, 2018
bfda8dc
Merge branch 'dev' into 1520_app_update_notif
krtkush Dec 5, 2018
67499bd
Merge branch 'dev' into 1520_app_update_notif
krtkush Dec 9, 2018
b674006
Conflict resolution.
krtkush Dec 28, 2018
794c370
Merge branch '1520_app_update_notif' of https://github.com/krtkush/Ne…
krtkush Dec 28, 2018
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
17 changes: 17 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ android {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}

buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

debug {
multiDexEnabled true

Expand All @@ -34,10 +36,25 @@ android {
// but continue the build even when errors are found:
abortOnError false
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

flavorDimensions "apkSource"
productFlavors {
github {
dimension "apkSource"
applicationIdSuffix ".github"

}

fdroid {
dimension "apkSource"
applicationIdSuffix ".fdroid"
}
}
}

ext {
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
public class App extends Application {
protected static final String TAG = App.class.toString();
private RefWatcher refWatcher;
private static App context;

@SuppressWarnings("unchecked")
private static final Class<? extends ReportSenderFactory>[] reportSenderFactoryClasses = new Class[]{AcraReportSenderFactory.class};
Expand All @@ -85,6 +86,8 @@ public void onCreate() {
}
refWatcher = installLeakCanary();

context = this;
TobiGr marked this conversation as resolved.
Show resolved Hide resolved

// Initialize settings first because others inits can use its values
SettingsActivity.initSettings(this);

Expand All @@ -96,6 +99,9 @@ public void onCreate() {
ImageLoader.getInstance().init(getImageLoaderConfigurations(10, 50));

configureRxJavaErrorHandler();

// Check for new version
new CheckForNewAppVersionTask().execute();
}

protected Downloader getDownloader() {
Expand Down Expand Up @@ -202,6 +208,25 @@ public void initNotificationChannel() {

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);

// Set up notification channel for app update only if it's a github apk.
if (BuildConfig.FLAVOR.equals(getString(R.string.app_flavor_github))) {

final String appUpdateId
= getString(R.string.app_update_notification_channel_id);
final CharSequence appUpdateName
= getString(R.string.app_update_notification_channel_name);
final String appUpdateDescription
= getString(R.string.app_update_notification_channel_description);

NotificationChannel appUpdateChannel
= new NotificationChannel(appUpdateId, appUpdateName, importance);
appUpdateChannel.setDescription(appUpdateDescription);

NotificationManager appUpdateNotificationManager
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
appUpdateNotificationManager.createNotificationChannel(appUpdateChannel);
}
}

@Nullable
Expand All @@ -217,4 +242,8 @@ protected RefWatcher installLeakCanary() {
protected boolean isDisposedRxExceptionsReported() {
return false;
}

public static App getContext() {
TobiGr marked this conversation as resolved.
Show resolved Hide resolved
return context;
}
}
162 changes: 162 additions & 0 deletions app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package org.schabi.newpipe;

import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
TobiGr marked this conversation as resolved.
Show resolved Hide resolved

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
* AsyncTask to check if there is a newer version of the NewPipe github apk available or not.
* If there is a newer version we show a notification, informing the user. On tapping
* the notification, the user will be directed to the download link.
*/
public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {

private Application app = App.getContext();

private String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json";
private int timeoutPeriod = 10000;

@Override
protected void onPreExecute() {
// Continue with version check only if the build variant is of type "github".
if (!BuildConfig.FLAVOR.equals(app.getString(R.string.app_flavor_github))) {
this.cancel(true);
}
}

@Override
protected String doInBackground(Void... voids) {

// Make a network request to get latest NewPipe data.

String response;
HttpURLConnection connection = null;

try {

URL url = new URL(newPipeApiUrl);
TobiGr marked this conversation as resolved.
Show resolved Hide resolved

connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(timeoutPeriod);
connection.setReadTimeout(timeoutPeriod);
connection.setRequestProperty("Content-length", "0");
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
connection.connect();

int responseStatus = connection.getResponseCode();

switch (responseStatus) {

case 200:
case 201:
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));

StringBuilder stringBuilder = new StringBuilder();

String line;

while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
}

bufferedReader.close();
response = stringBuilder.toString();

return response;
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
krtkush marked this conversation as resolved.
Show resolved Hide resolved
ex.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

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

} finally {
if (connection != null) {
try {
connection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

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

}
}
}

return null;
}

@Override
protected void onPostExecute(String response) {

Log.i("Response--", response);
Copy link
Member

Choose a reason for hiding this comment

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

This entry does not help us to find issues in the future. Please add some additional information or remove this log entry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was not supposed to go into production. I'll remove it.


// Parse the json from the response.
if (response != null) {

try {
JSONObject mainObject = new JSONObject(response);
JSONObject flavoursObject = mainObject.getJSONObject("flavors");
JSONObject githubObject = flavoursObject.getJSONObject("github");
JSONObject githubStableObject = githubObject.getJSONObject("stable");

String versionName = githubStableObject.getString("version");
String versionCode = githubStableObject.getString("version_code");
String apkLocationUrl = githubStableObject.getString("apk");

compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode);

} catch (JSONException ex) {
ex.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have used ErrorActivity.reportError() in every case as I found it more appropriate against CheckForNewAppVersionTask.

}
}
}

/**
* Method to compare the current and latest available app version.
* If a newer version is available, we show the update notification.
* @param versionName
* @param apkLocationUrl
*/
private void compareAppVersionAndShowNotification(String versionName,
String apkLocationUrl,
String versionCode) {

int NOTIFICATION_ID = 2000;

if (BuildConfig.VERSION_CODE < Integer.valueOf(versionCode)) {

// A pending intent to open the apk location url in the browser.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
PendingIntent pendingIntent
= PendingIntent.getActivity(app, 0, intent, 0);

NotificationCompat.Builder notificationBuilder = new NotificationCompat
.Builder(app, app.getString(R.string.app_update_notification_channel_id))
.setSmallIcon(R.drawable.ic_newpipe_update)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setContentTitle(app.getString(R.string.app_update_notification_content_title))
.setContentText(app.getString(R.string.app_update_notification_content_text)
+ " " + versionName);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.android.exoplayer2.ui.SubtitleView;

import org.schabi.newpipe.BuildConfig;
import org.schabi.newpipe.CheckForNewAppVersionTask;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.VideoStream;
Expand Down Expand Up @@ -663,6 +664,9 @@ public void onPlaying() {
lockManager.acquireWifiAndCpu();

hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);

// Check for new version
new CheckForNewAppVersionTask().execute();
Copy link
Member

Choose a reason for hiding this comment

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

Why do you run the check here, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I remember the check was not being performed in the pop-up mode. Hence, I had to put it here. I'll check once again and make changes if needed.

}

@Override
Expand Down
Binary file added app/src/main/res/drawable-hdpi/ic_newpipe_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_newpipe_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@
<string name="notification_channel_name">NewPipe Notification</string>
<string name="notification_channel_description">Notifications for NewPipe Background and Popup Players</string>

<string name="app_update_notification_channel_id" translatable="false">newpipeAppUpdate</string>
<string name="app_update_notification_channel_name">App Update Notification</string>
<string name="app_update_notification_channel_description">Notifications for new NewPipe version</string>

<string name="unknown_content">[Unknown]</string>

<string name="toggle_orientation">Toggle Orientation</string>
Expand Down Expand Up @@ -504,4 +508,12 @@
<item>144p</item>
</string-array>

<!-- App update notification -->
<string name="app_update_notification_content_title">NewPipe Update Available</string>
<string name="app_update_notification_content_text">Tap to download</string>

<!-- Flavor names -->
<string name="app_flavor_github">github</string>
<string name="app_flavor_fdroid">fdroid</string>

</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.1.4'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down