Handle installation requests from DownloadService

This is needed to protect the instantiation of DownloadController,
which will be done with a follow-up change.
This commit is contained in:
Gabriele M
2017-07-03 16:08:33 +02:00
parent dc95edbf80
commit dcbcc759d2
3 changed files with 16 additions and 60 deletions

View File

@@ -28,7 +28,6 @@
<service android:name=".DownloadService" />
<receiver android:name=".UpdaterBroadcastReceiver" android:exported="false" />
<receiver android:name=".UpdatesCheckReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>

View File

@@ -30,6 +30,9 @@ import android.text.format.DateUtils;
import android.text.format.Formatter;
import android.util.Log;
import org.lineageos.updater.misc.Utils;
import java.io.IOException;
import java.text.NumberFormat;
public class DownloadService extends Service {
@@ -39,6 +42,7 @@ public class DownloadService extends Service {
public static final String ACTION_DOWNLOAD_CONTROL = "action_download_control";
public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
public static final String EXTRA_DOWNLOAD_CONTROL = "extra_download_control";
public static final String ACTION_INSTALL_UPDATE = "action_install_update";
public static final int DOWNLOAD_RESUME = 0;
public static final int DOWNLOAD_PAUSE = 1;
@@ -152,6 +156,14 @@ public class DownloadService extends Service {
} else {
Log.e(TAG, "Unknown download action");
}
} else if (ACTION_INSTALL_UPDATE.equals(intent.getAction())) {
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
try {
Utils.triggerUpdate(this, mDownloadController.getUpdate(downloadId));
} catch (IOException e) {
Log.e(TAG, "Could not install update");
// TODO: user facing message
}
}
Log.d(TAG, "Service started");
return START_STICKY;
@@ -285,10 +297,10 @@ public class DownloadService extends Service {
}
private PendingIntent getInstallPendingIntent(String downloadId) {
final Intent intent = new Intent(this, UpdaterBroadcastReceiver.class);
intent.setAction(UpdaterBroadcastReceiver.ACTION_INSTALL_UPDATE);
intent.putExtra(UpdaterBroadcastReceiver.EXTRA_DOWNLOAD_ID, downloadId);
return PendingIntent.getBroadcast(this, 0, intent,
final Intent intent = new Intent(this, DownloadService.class);
intent.setAction(ACTION_INSTALL_UPDATE);
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
return PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
}
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright (C) 2017 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.updater;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import org.lineageos.updater.misc.Utils;
import java.io.IOException;
public class UpdaterBroadcastReceiver extends BroadcastReceiver {
public static final String EXTRA_DOWNLOAD_ID =
"org.lineageos.updater.extra.DOWNLOAD_ID";
public static final String ACTION_INSTALL_UPDATE =
"org.lineageos.updater.action.INSTALL_UPDATE";
private final static String TAG = "BroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (ACTION_INSTALL_UPDATE.equals(action)) {
if (!intent.hasExtra(EXTRA_DOWNLOAD_ID)) {
Log.e(TAG, "Missing download ID");
return;
}
DownloadController downloadController = DownloadController.getInstance(context);
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
UpdateDownload update = downloadController.getUpdate(downloadId);
try {
Utils.triggerUpdate(context, update);
} catch (IOException e) {
Log.e(TAG, "Could not trigger update");
// TODO: show error
}
}
}
}