Skip to content

Commit

Permalink
Add remote_plugin function to Easer
Browse files Browse the repository at this point in the history
  • Loading branch information
renyuneyun committed Sep 16, 2018
1 parent 5eb60c2 commit f795f5d
Show file tree
Hide file tree
Showing 172 changed files with 2,152 additions and 477 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':remote_plugin')

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.Test;

import ryey.easer.commons.C;
import ryey.easer.commons.PluginDataFormat;
import ryey.easer.plugins.TestHelper;

import static org.junit.Assert.assertEquals;
Expand All @@ -42,7 +43,7 @@ public void testParcel() {
public void testSerialize() throws Exception {
BroadcastOperationDataFactory factory = new BroadcastOperationDataFactory();
BroadcastOperationData dummyData = factory.dummyData();
for (C.Format format : C.Format.values()) {
for (PluginDataFormat format : PluginDataFormat.values()) {
String serialized_data = dummyData.serialize(format);
BroadcastOperationData parsed_data = factory.parse(serialized_data, format, C.VERSION_CURRENT);
assertEquals(dummyData, parsed_data);
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@
android:name="android.appwidget.provider"
android:resource="@xml/easer_status_widget_info" />
</receiver>

<service
android:name=".core.RemotePluginRegistryService"
android:enabled="true"
android:exported="false"
android:process=":RemotePlugin"></service>
</application>

</manifest>
2 changes: 2 additions & 0 deletions app/src/main/java/ryey/easer/EaserApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.orhanobut.logger.Logger;

import ryey.easer.core.ProfileLoaderService;
import ryey.easer.core.RemotePluginRegistryService;
import ryey.easer.core.log.ActivityLogService;

public class EaserApplication extends Application {
Expand All @@ -53,6 +54,7 @@ public void onCreate() {

startService(new Intent(this, ActivityLogService.class));
startService(new Intent(this, ProfileLoaderService.class));
startService(new Intent(this, RemotePluginRegistryService.class));

Logger.log(Logger.ASSERT, null, "======Easer started======", null);
}
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/ryey/easer/commons/C.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,4 @@ public class C {
public static final int VERSION_CREATED_IN_RUNTIME = -1;
public static final int VERSION_CURRENT = VERSION_ADD_DYNAMICS;

public enum Format {
JSON,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,51 @@

package ryey.easer.commons;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.Locale;

import ryey.easer.commons.plugindef.PluginDef;
import ryey.easer.commons.plugindef.conditionplugin.ConditionPlugin;
import ryey.easer.commons.plugindef.eventplugin.EventPlugin;
import ryey.easer.commons.plugindef.operationplugin.OperationPlugin;

public class CommonHelper {
public class CommonPluginHelper {

public static final int TYPE_OPERATION = 0;
public static final int TYPE_EVENT = 1;
public static final int TYPE_CONDITION = 2;
private static final String[] TYPE_NAMES = {"operation", "event", "condition"};

public static String pluginEnabledKey(PluginDef plugin) {
String type_name;
if (plugin instanceof OperationPlugin)
type_name = "operation";
type_name = TYPE_NAMES[0];
else if (plugin instanceof EventPlugin)
type_name = "event";
type_name = TYPE_NAMES[1];
else if (plugin instanceof ConditionPlugin)
type_name = "condition";
type_name = TYPE_NAMES[2];
else
throw new IllegalAccessError("Unknown plugin type???");
return String.format(Locale.US, "%s_plugin_enabled_%s", type_name, plugin.name());
return String.format(Locale.US, "%s_plugin_enabled_%s", type_name, plugin.id());
}

public static String pluginEnabledKey(int type, String id) {
return String.format(Locale.US, "%s_plugin_enabled_%s", TYPE_NAMES[type], id);
}

/**
* This method assumes the given id is corresponded to a plugin
* @param context
* @param type
* @param id
* @return
*/
public static boolean isEnabled(Context context, int type, String id) {
SharedPreferences settingsPreference =
PreferenceManager.getDefaultSharedPreferences(context);
return settingsPreference.getBoolean(CommonPluginHelper.pluginEnabledKey(type, id), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* Base Fragment class for plugin's UI.
*/
public abstract class PluginViewFragment<T extends StorageData> extends Fragment {
public abstract class PluginViewFragment<T extends StorageData> extends PluginViewFragmentInterface<T> {

/**
* Used in case {@link #onCreateView} is called after {@link #fill}`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import android.support.annotation.NonNull;

import ryey.easer.commons.C;
import ryey.easer.commons.IllegalStorageDataException;
import ryey.easer.commons.PluginDataFormat;
import ryey.easer.commons.plugindef.DataFactory;
import ryey.easer.commons.plugindef.ValidData;

Expand All @@ -37,5 +37,5 @@ public interface EventDataFactory<T extends EventData> extends DataFactory<T> {

@ValidData
@NonNull
T parse(@NonNull String data, @NonNull C.Format format, int version) throws IllegalStorageDataException;
T parse(@NonNull String data, @NonNull PluginDataFormat format, int version) throws IllegalStorageDataException;
}
29 changes: 23 additions & 6 deletions app/src/main/java/ryey/easer/core/ProfileLoaderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
import ryey.easer.commons.plugindef.operationplugin.OperationLoader;
import ryey.easer.commons.plugindef.operationplugin.OperationPlugin;
import ryey.easer.core.data.ProfileStructure;
import ryey.easer.core.data.RemoteLocalOperationDataWrapper;
import ryey.easer.core.data.storage.ProfileDataStorage;
import ryey.easer.core.dynamics.CoreDynamics;
import ryey.easer.core.dynamics.CoreDynamicsInterface;
import ryey.easer.core.log.ActivityLogService;
import ryey.easer.plugins.PluginRegistry;
import ryey.easer.remote_plugin.RemoteOperationData;

import static ryey.easer.core.Lotus.EXTRA_DYNAMICS_LINK;
import static ryey.easer.core.Lotus.EXTRA_DYNAMICS_PROPERTIES;
Expand Down Expand Up @@ -72,6 +74,8 @@ public static void triggerProfile(Context context, String profileName, String sc
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

private RemotePluginCommunicationHelper helper;

private IntentFilter intentFilter = new IntentFilter(ACTION_LOAD_PROFILE);
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
Expand All @@ -95,10 +99,13 @@ public void onReceive(Context context, Intent intent) {
public void onCreate() {
Logger.v("ProfileLoaderService onCreate()");
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
helper = new RemotePluginCommunicationHelper(this);
helper.begin();
}

@Override
public void onDestroy() {
helper.end();
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}

Expand Down Expand Up @@ -129,19 +136,29 @@ private void handleActionLoadProfile(@NonNull String name, @Nullable String even

if (profile != null) {
boolean loaded = false;
for (OperationPlugin plugin : PluginRegistry.getInstance().operation().getEnabledPlugins(this)) {
OperationLoader loader = plugin.loader(getApplicationContext());
Collection<OperationData> possibleData = profile.get(plugin.id());
if (possibleData != null) {
for (OperationData data : possibleData) {
PluginRegistry.Registry<OperationPlugin, OperationData> operationRegistry = PluginRegistry.getInstance().operation();
for (String pluginId : profile.pluginIds()) {
Collection<RemoteLocalOperationDataWrapper> dataCollection = profile.get(pluginId);
if (operationRegistry.hasPlugin(pluginId)) {
OperationPlugin plugin = operationRegistry.findPlugin(pluginId);
assert plugin != null;
OperationLoader loader = plugin.loader(getApplicationContext());
for (RemoteLocalOperationDataWrapper data : dataCollection) {
OperationData localData = data.localData;
assert localData != null;
try {
//noinspection unchecked
if (loader.load(data.applyDynamics(solidMacroAssignment)))
if (loader.load(localData.applyDynamics(solidMacroAssignment)))
loaded = true;
} catch (RuntimeException e) {
Logger.e(e, "error while loading operation <%s> for profile <%s>", data.getClass().getSimpleName(), profile.getName());
}
}
} else {
for (RemoteLocalOperationDataWrapper data : dataCollection) {
RemoteOperationData remoteData = data.remoteData;
helper.asyncTriggerOperation(pluginId, remoteData);
}
}
}
String extraInfo;
Expand Down
66 changes: 66 additions & 0 deletions app/src/main/java/ryey/easer/core/RemoteOperationPluginInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2016 - 2018 Rui Zhao <[email protected]>
*
* This file is part of Easer.
*
* Easer 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.
*
* Easer 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.
*
* You should have received a copy of the GNU General Public License
* along with Easer. If not, see <http://www.gnu.org/licenses/>.
*/

package ryey.easer.core

import android.os.Parcel
import android.os.Parcelable
import ryey.easer.commons.plugindef.operationplugin.Category
import ryey.easer.remote_plugin.RemotePlugin

class RemoteOperationPluginInfo(packageName: String, pluginId: String,
pluginName: String, val category: Category)
: RemotePluginInfo(packageName, pluginId, pluginName, RemotePlugin.TYPE_OPERATION_PLUGIN) {

constructor(packageName: String, pluginId: String,
pluginName: String, category: String)
: this(packageName, pluginId, pluginName,
try {
Category.valueOf(category)
} catch (e: Exception) {
Category.unknown
})

constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString())

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(packageName)
parcel.writeString(pluginId)
parcel.writeString(pluginName)
parcel.writeString(category.toString())
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<RemoteOperationPluginInfo> {
override fun createFromParcel(parcel: Parcel): RemoteOperationPluginInfo {
return RemoteOperationPluginInfo(parcel)
}

override fun newArray(size: Int): Array<RemoteOperationPluginInfo?> {
return arrayOfNulls(size)
}
}
}
Loading

0 comments on commit f795f5d

Please sign in to comment.