2012-03-23 18:03:48 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-24 20:17:46 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-03-23 18:03:48 +00:00
|
|
|
|
|
|
|
package org.mozilla.gecko.sync;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2012-03-28 03:09:14 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2012-03-23 18:03:48 +00:00
|
|
|
|
|
|
|
import org.json.simple.JSONArray;
|
2012-03-28 03:09:14 +00:00
|
|
|
import org.mozilla.gecko.R;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2012-03-23 18:03:48 +00:00
|
|
|
|
|
|
|
public class CommandProcessor {
|
|
|
|
private static final String LOG_TAG = "Command";
|
2012-03-28 03:09:14 +00:00
|
|
|
private static AtomicInteger currentId = new AtomicInteger();
|
2012-03-23 18:03:48 +00:00
|
|
|
protected ConcurrentHashMap<String, CommandRunner> commands = new ConcurrentHashMap<String, CommandRunner>();
|
|
|
|
|
|
|
|
private final static CommandProcessor processor = new CommandProcessor();
|
|
|
|
|
|
|
|
public static CommandProcessor getProcessor() {
|
|
|
|
return processor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Command {
|
|
|
|
public final String commandType;
|
2012-06-10 23:37:47 +00:00
|
|
|
public final List<String> args;
|
2012-03-23 18:03:48 +00:00
|
|
|
|
2012-06-10 23:37:47 +00:00
|
|
|
public Command(String commandType, List<String> args) {
|
2012-03-23 18:03:48 +00:00
|
|
|
this.commandType = commandType;
|
|
|
|
this.args = args;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerCommand(String commandType, CommandRunner command) {
|
|
|
|
commands.put(commandType, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processCommand(ExtendedJSONObject unparsedCommand) {
|
|
|
|
Command command = parseCommand(unparsedCommand);
|
|
|
|
if (command == null) {
|
|
|
|
Logger.debug(LOG_TAG, "Invalid command: " + unparsedCommand + " will not be processed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandRunner executableCommand = commands.get(command.commandType);
|
|
|
|
if (executableCommand == null) {
|
|
|
|
Logger.debug(LOG_TAG, "Command \"" + command.commandType + "\" not registered and will not be processed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-10 23:37:47 +00:00
|
|
|
executableCommand.executeCommand(command.args);
|
2012-03-23 18:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a JSON command into a ParsedCommand object for easier handling.
|
|
|
|
*
|
|
|
|
* @param unparsedCommand - command as ExtendedJSONObject
|
|
|
|
* @return - null if command is invalid, else return ParsedCommand with
|
|
|
|
* no null attributes.
|
|
|
|
*/
|
2012-06-10 23:37:47 +00:00
|
|
|
protected Command parseCommand(ExtendedJSONObject unparsedCommand) {
|
2012-03-23 18:03:48 +00:00
|
|
|
String type = (String) unparsedCommand.get("command");
|
|
|
|
if (type == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONArray unparsedArgs = unparsedCommand.getArray("args");
|
|
|
|
if (unparsedArgs == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-06-10 23:37:47 +00:00
|
|
|
ArrayList<String> args = new ArrayList<String>(unparsedArgs.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < unparsedArgs.size(); i++) {
|
|
|
|
args.add(unparsedArgs.get(i).toString());
|
|
|
|
}
|
2012-03-23 18:03:48 +00:00
|
|
|
|
2012-06-10 23:37:47 +00:00
|
|
|
return new Command(type, args);
|
2012-03-23 18:03:48 +00:00
|
|
|
} catch (NonArrayJSONException e) {
|
|
|
|
Logger.debug(LOG_TAG, "Unable to parse args array. Invalid command");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 03:09:14 +00:00
|
|
|
|
2012-06-10 23:37:47 +00:00
|
|
|
public void displayURI(List<String> args, Context context) {
|
|
|
|
// These two args are guaranteed to exist by trusting the client sender.
|
|
|
|
String uri = args.get(0);
|
|
|
|
String clientId = args.get(1);
|
2012-03-28 03:09:14 +00:00
|
|
|
|
2012-06-10 23:37:47 +00:00
|
|
|
Logger.info(LOG_TAG, "Received a URI for display: " + uri + " from " + clientId);
|
2012-03-28 03:09:14 +00:00
|
|
|
|
|
|
|
String title = null;
|
|
|
|
if (args.size() == 3) {
|
|
|
|
title = args.get(2);
|
|
|
|
}
|
|
|
|
|
2012-06-10 23:37:47 +00:00
|
|
|
// Get NotificationManager.
|
|
|
|
String ns = Context.NOTIFICATION_SERVICE;
|
|
|
|
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);
|
2012-03-28 03:09:14 +00:00
|
|
|
|
2012-06-10 23:37:47 +00:00
|
|
|
// Create a Notficiation.
|
|
|
|
int icon = R.drawable.sync_ic_launcher;
|
2012-03-28 03:09:14 +00:00
|
|
|
String notificationTitle = context.getString(R.string.sync_new_tab);
|
|
|
|
if (title != null) {
|
|
|
|
notificationTitle = notificationTitle.concat(": " + title);
|
|
|
|
}
|
2012-06-10 23:37:47 +00:00
|
|
|
long when = System.currentTimeMillis();
|
2012-03-28 03:09:14 +00:00
|
|
|
Notification notification = new Notification(icon, notificationTitle, when);
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
|
|
|
|
|
|
|
// Set pending intent associated with the notification.
|
|
|
|
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
|
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
|
|
|
|
notification.setLatestEventInfo(context, notificationTitle, uri, contentIntent);
|
|
|
|
|
|
|
|
// Send notification.
|
2012-06-10 23:37:47 +00:00
|
|
|
mNotificationManager.notify(currentId.getAndIncrement(), notification);
|
2012-03-28 03:09:14 +00:00
|
|
|
}
|
2012-03-23 18:03:48 +00:00
|
|
|
}
|