Skip to content

Commit

Permalink
Show a changelog.
Browse files Browse the repository at this point in the history
* Changelog is HTML, shown via HTMLViewer, which is also used by
  Settings -> About -> Legal
* Changelog file: <device>-<channel>-changelog.html
* Show changelog if it exists upon tapping the notification, or
  the settings entry.

Potential TODOs:
* The view can be zoomed in and out (which may be useful),
  and also scrolled around easily (not so useful).
  • Loading branch information
Chirayu Desai committed Aug 22, 2019
1 parent 2536b88 commit dd507c7
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 5 deletions.
10 changes: 10 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,15 @@
<action android:name="android.settings.SYSTEM_UPDATE_SETTINGS" />
</intent-filter>
</activity>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="app.seamlessupdate.client.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
3 changes: 3 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
<string name="idle_reboot_summary">Automatically reboot once the device is idle after successfully installing an update</string>
<string name="check_for_updates_title">Updates</string>
<string name="check_for_updates_summary">Tap to check for updates</string>
<string name="changelog_title">Changelog</string>
<string name="changelog_summary">Tap to view changelog</string>
<string name="changelog_unavailable">Changelog unavailable</string>
</resources>
4 changes: 4 additions & 0 deletions res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="current.html" path="changelog/" />
</paths>
4 changes: 4 additions & 0 deletions res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
android:title="@string/check_for_updates_title"
android:summary="@string/check_for_updates_summary" />

<Preference android:key="changelog"
android:title="@string/changelog_title"
android:summary="@string/changelog_summary" />

<ListPreference android:key="channel"
android:title="@string/channel_title"
android:summary="%s"
Expand Down
21 changes: 16 additions & 5 deletions src/app/seamlessupdate/client/NotificationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.FileProvider;

import java.io.File;

import static android.app.NotificationManager.IMPORTANCE_LOW;
import static app.seamlessupdate.client.Service.isAbUpdate;
Expand All @@ -19,6 +24,7 @@ public class NotificationHandler {
private static final String NOTIFICATION_CHANNEL_ID_PROGRESS = "progress";
private static final int PENDING_REBOOT_ID = 1;
private static final int PENDING_SETTINGS_ID = 2;
private static final int PENDING_CHANGELOG_ID = 3;

private final Context context;
private final NotificationManager notificationManager;
Expand All @@ -32,7 +38,7 @@ public class NotificationHandler {
void showDownloadNotification(int progress, int max) {
String title = context.getString(R.string.notification_download_title);
Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID_PROGRESS)
.setContentIntent(getPendingSettingsIntent())
.setContentIntent(getPendingChangelogIntent())
.setContentTitle(title)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_system_update_white_24dp);
Expand All @@ -58,7 +64,7 @@ void showRebootNotification() {
notificationManager.createNotificationChannel(channel);
notificationManager.notify(NOTIFICATION_ID_REBOOT, new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
.addAction(R.drawable.ic_restart, rebootText, reboot)
.setContentIntent(getPendingSettingsIntent())
.setContentIntent(getPendingChangelogIntent())
.setContentTitle(title)
.setContentText(text)
.setOngoing(true)
Expand All @@ -69,7 +75,7 @@ void showRebootNotification() {
void showInstallNotification(int progress, int max) {
String title = context.getString(R.string.notification_install_title);
Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID_PROGRESS)
.setContentIntent(getPendingSettingsIntent())
.setContentIntent(getPendingChangelogIntent())
.setContentTitle(title)
.setOngoing(true)
.setProgress(max, progress, false)
Expand All @@ -87,8 +93,13 @@ private void createProgressNotificationChannel() {
notificationManager.createNotificationChannel(channel);
}

private PendingIntent getPendingSettingsIntent() {
return PendingIntent.getActivity(context, PENDING_SETTINGS_ID, new Intent(context, Settings.class), 0);
private PendingIntent getPendingChangelogIntent() {
Intent intent = Settings.getChangelogIntent(context, "current.html");
if (intent != null) {
return PendingIntent.getActivity(context, PENDING_CHANGELOG_ID, intent, 0);
} else {
return null;
}
}

}
24 changes: 24 additions & 0 deletions src/app/seamlessupdate/client/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static android.os.UpdateEngine.UpdateStatusConstants.FINALIZING;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
Expand Down Expand Up @@ -255,6 +256,8 @@ protected void onHandleIntent(final Intent intent) {
return;
}

downloadChangelog(this, channel);

String downloadFile = preferences.getString(PREFERENCE_DOWNLOAD_FILE, null);
long downloaded = UPDATE_PATH.length();
int contentLength;
Expand Down Expand Up @@ -327,4 +330,25 @@ protected void onHandleIntent(final Intent intent) {
TriggerUpdateReceiver.completeWakefulIntent(intent);
}
}

private void downloadChangelog(Context context, String channel) {
Log.d(TAG, "fetching changelog for " + DEVICE + "-" + channel);
final String changelogName = DEVICE + "-" + channel + "-changelog.html";
try {
final File changelogPath = new File(context.getCacheDir(), "changelog");
final File changelogFile = new File(changelogPath, "current.html");
changelogPath.mkdirs();
InputStream input = fetchData(changelogName).getInputStream();
final OutputStream output = new FileOutputStream(changelogFile);
int bytesRead;
final byte[] buffer = new byte[8192];
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
output.close();
input.close();
} catch (IOException e) {
Log.d(TAG, "failed to fetch changelog");
}
}
}
42 changes: 42 additions & 0 deletions src/app/seamlessupdate/client/Settings.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package app.seamlessupdate.client;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.UserManager;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.net.Uri;
import android.support.v4.content.FileProvider;

import java.io.File;

public class Settings extends PreferenceActivity {
private static final String KEY_CHANNEL = "channel";
private static final String KEY_NETWORK_TYPE = "network_type";
private static final String KEY_BATTERY_NOT_LOW = "battery_not_low";
private static final String KEY_IDLE_REBOOT = "idle_reboot";
private static final String KEY_CHECK_FOR_UDPATES = "check_for_updates";
private static final String KEY_CHANGELOG = "changelog";
static final String KEY_WAITING_FOR_REBOOT = "waiting_for_reboot";

static SharedPreferences getPreferences(final Context context) {
Expand Down Expand Up @@ -61,6 +68,17 @@ public void onCreate(final Bundle savedInstanceState) {
return true;
});

final Preference changelog = findPreference(KEY_CHANGELOG);
changelog.setOnPreferenceClickListener((final Preference preference) -> {
Intent intent = getChangelogIntent(this, "current.html");
if (intent != null) {
startActivity(intent);
} else {
changelog.setSummary(getString(R.string.changelog_unavailable));
}
return true;
});

final Preference channel = findPreference(KEY_CHANNEL);
channel.setOnPreferenceChangeListener((final Preference preference, final Object newValue) -> {
getPreferences(this).edit().putString(KEY_CHANNEL,(String) newValue).apply();
Expand Down Expand Up @@ -104,5 +122,29 @@ public void onResume() {
super.onResume();
final ListPreference networkType = (ListPreference) findPreference(KEY_NETWORK_TYPE);
networkType.setValue(Integer.toString(getNetworkType(this)));
final Preference changelog = findPreference(KEY_CHANGELOG);
changelog.setSummary(getString(R.string.changelog_summary));
}

static Intent getChangelogIntent(Context context, String filename) {
// From com.android.settings.SettingsLicenseActivity.showHtmlFromUri()
// Kick off external viewer due to WebView security restrictions; we
// carefully point it at HTMLViewer, since it offers to decompress
// before viewing.
final File changelogPath = new File(context.getCacheDir(), "changelog");
final File changelogFile = new File(changelogPath, filename);
if (!changelogFile.exists()) {
return null;
}
final Uri uri = FileProvider.getUriForFile(context, "app.seamlessupdate.client.fileprovider", changelogFile);
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "text/html");
intent.putExtra(Intent.EXTRA_TITLE, context.getString(R.string.changelog_title));
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setPackage("com.android.htmlviewer");
return intent;
}
}

0 comments on commit dd507c7

Please sign in to comment.