Make InstallerService an IntentService
Because only one apk should be installed at a time and no cancelation is required it is sufficient to use an IntentService
This commit is contained in:
parent
d6803e1bf4
commit
13f2e30a40
@ -1,118 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
* Copyright (C) 2016 Hans-Christoph Steiner
|
||||
* Copyright (C) 2016 Dominik Schürmann <dominik@dominikschuermann.de>
|
||||
*
|
||||
* 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
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 3
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* 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.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package org.fdroid.fdroid.installer;
|
||||
|
||||
import android.app.Service;
|
||||
import android.app.IntentService;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.IBinder;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.os.Process;
|
||||
import android.util.Log;
|
||||
|
||||
import org.fdroid.fdroid.Utils;
|
||||
|
||||
/**
|
||||
* InstallerService based on DownloaderService
|
||||
*/
|
||||
public class InstallerService extends Service {
|
||||
private static final String TAG = "InstallerService";
|
||||
public class InstallerService extends IntentService {
|
||||
|
||||
private static final String ACTION_INSTALL = "org.fdroid.fdroid.installer.InstallerService.action.INSTALL";
|
||||
private static final String ACTION_UNINSTALL = "org.fdroid.fdroid.installer.InstallerService.action.UNINSTALL";
|
||||
|
||||
private volatile Looper serviceLooper;
|
||||
private static volatile ServiceHandler serviceHandler;
|
||||
|
||||
private final class ServiceHandler extends Handler {
|
||||
ServiceHandler(Looper looper) {
|
||||
super(looper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
Utils.debugLog(TAG, "Handling message with ID of " + msg.what);
|
||||
handleIntent((Intent) msg.obj);
|
||||
stopSelf(msg.arg1);
|
||||
}
|
||||
public InstallerService() {
|
||||
super("InstallerService");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Utils.debugLog(TAG, "Creating installer service.");
|
||||
|
||||
HandlerThread thread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
|
||||
thread.start();
|
||||
|
||||
serviceLooper = thread.getLooper();
|
||||
serviceHandler = new ServiceHandler(serviceLooper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Utils.debugLog(TAG, "Received Intent for installing/uninstalling: " + intent + " (with a startId of " + startId + ")");
|
||||
|
||||
if (ACTION_INSTALL.equals(intent.getAction())) {
|
||||
Uri uri = intent.getData();
|
||||
|
||||
Message msg = serviceHandler.obtainMessage();
|
||||
msg.arg1 = startId;
|
||||
msg.obj = intent;
|
||||
msg.what = uri.hashCode();
|
||||
serviceHandler.sendMessage(msg);
|
||||
Utils.debugLog(TAG, "Start install of " + uri.toString());
|
||||
} else if (ACTION_UNINSTALL.equals(intent.getAction())) {
|
||||
String packageName = intent.getStringExtra(Installer.EXTRA_PACKAGE_NAME);
|
||||
|
||||
Message msg = serviceHandler.obtainMessage();
|
||||
msg.arg1 = startId;
|
||||
msg.obj = intent;
|
||||
msg.what = packageName.hashCode();
|
||||
serviceHandler.sendMessage(msg);
|
||||
Utils.debugLog(TAG, "Start uninstall of " + packageName);
|
||||
} else {
|
||||
Log.e(TAG, "Received Intent with unknown action: " + intent);
|
||||
}
|
||||
|
||||
return START_REDELIVER_INTENT; // if killed before completion, retry Intent
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Utils.debugLog(TAG, "Destroying installer service. Will move to background and stop our Looper.");
|
||||
serviceLooper.quit(); //NOPMD - this is copied from IntentService, no super call needed
|
||||
}
|
||||
|
||||
/**
|
||||
* This service does not use binding, so no need to implement this method
|
||||
*/
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void handleIntent(Intent intent) {
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
String packageName = intent.getStringExtra(Installer.EXTRA_PACKAGE_NAME);
|
||||
Installer installer = InstallerFactory.create(this, packageName);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user