Skip to content

Commit

Permalink
finish adding HotspotOperation
Browse files Browse the repository at this point in the history
fix #30
  • Loading branch information
renyuneyun committed Sep 11, 2017
1 parent 35bb624 commit f804065
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/ryey/easer/EaserApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Application;

import ryey.easer.core.EHService;
import ryey.easer.plugins.PluginRegistry;

public class EaserApplication extends Application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package ryey.easer.commons.plugindef.operationplugin;

import android.content.Context;
import android.util.Log;

/*
* Loader of a operation plugin.
Expand All @@ -32,5 +33,12 @@ public OperationLoader(Context context) {
this.context = context;
}

public abstract boolean load(OperationData data);
public boolean load(OperationData data) {
Log.d(getClass().getSimpleName(), String.format("loading data %s", data));
boolean ret = _load(data);
Log.d(getClass().getSimpleName(), String.format("data %sloaded", ret ? "" : "not "));
return ret;
}

protected abstract boolean _load(OperationData data);
}
2 changes: 2 additions & 0 deletions app/src/main/java/ryey/easer/plugins/PluginRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import ryey.easer.plugins.operation.broadcast.BroadcastOperationPlugin;
import ryey.easer.plugins.operation.cellular.CellularOperationPlugin;
import ryey.easer.plugins.operation.command.CommandOperationPlugin;
import ryey.easer.plugins.operation.hotspot.HotspotOperationPlugin;
import ryey.easer.plugins.operation.ringer_mode.RingerModeOperationPlugin;
import ryey.easer.plugins.operation.rotation.RotationOperationPlugin;
import ryey.easer.plugins.operation.wifi.WifiOperationPlugin;
Expand Down Expand Up @@ -75,6 +76,7 @@ public static void init() {
pluginRegistry.registerOperationPlugin(BrightnessOperationPlugin.class);
pluginRegistry.registerOperationPlugin(RingerModeOperationPlugin.class);
pluginRegistry.registerOperationPlugin(CommandOperationPlugin.class);
pluginRegistry.registerOperationPlugin(HotspotOperationPlugin.class);
//TODO: register plugins

pluginRegistry.initialised.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public BluetoothLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
Boolean state = (Boolean) data.get();
if (state == null)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public BrightnessLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
if (data instanceof BrightnessOperationData)
return true;
return loadthis((BrightnessOperationData) data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public BroadcastLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
Log.d(getClass().getSimpleName(), "load");
IntentData iData = (IntentData) data.get();
Intent intent = new Intent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public CellularLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
Boolean state = (Boolean) data.get();
if (state == null)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CommandLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
String text = (String) data.get();
String []commands = text.split("\n");
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2016 - 2017 Rui Zhao <renyuneyun@gmail.com>
*
* 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.plugins.operation.hotspot;

import android.content.Context;

import ryey.easer.R;
import ryey.easer.commons.plugindef.StorageData;
import ryey.easer.plugins.reusable.SwitchContentLayout;

public class HotspotContentLayout extends SwitchContentLayout {

public HotspotContentLayout(Context context) {
super(context);
setDesc(context.getString(R.string.operation_hotspot));
}

@Override
public StorageData getData() {
return new HotspotOperationData(state());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ryey.easer.plugins.operation.hotspot;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class HotspotHelper {
private static HotspotHelper instance = null;
private WifiManager wifiManager;

static synchronized HotspotHelper getInstance(Context context) {
if (instance != null)
return instance;
instance = new HotspotHelper();
instance.wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
return instance;
}

boolean enableAp() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
boolean apStatus = setApStatus(null, true);
return apStatus;
}

boolean disableAp() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
boolean apStatus = setApStatus(null, false);
return apStatus;
}

boolean isApEnabled() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method isWifiApEnabledmethod = wifiManager.getClass().getMethod("isWifiApEnabled");
Boolean isWifiApEnabled = (boolean) isWifiApEnabledmethod.invoke(wifiManager);
return isWifiApEnabled;
}

WifiConfiguration getApConfiguration() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
WifiConfiguration netConfig = (WifiConfiguration) getWifiApConfigurationMethod.invoke(wifiManager);
return netConfig;
}

boolean setApStatus(WifiConfiguration netConfig, boolean enable) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apStatus = (boolean) setWifiApMethod.invoke(wifiManager, netConfig, enable);
return apStatus;
}

int getApState() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState");
int apState = (int) getWifiApStateMethod.invoke(wifiManager);
return apState;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2016 - 2017 Rui Zhao <renyuneyun@gmail.com>
*
* 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.plugins.operation.hotspot;

import android.content.Context;

import ryey.easer.commons.plugindef.operationplugin.OperationData;
import ryey.easer.commons.plugindef.operationplugin.OperationLoader;

public class HotspotLoader extends OperationLoader {
HotspotHelper helper;

public HotspotLoader(Context context) {
super(context);
helper = HotspotHelper.getInstance(context);
}

@Override
public boolean _load(OperationData data) {
Boolean state = (Boolean) data.get();
if (state == null)
return true;
try {
if (helper.isApEnabled() == state)
return true;
if (state)
return helper.enableAp();
else
return helper.disableAp();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2016 - 2017 Rui Zhao <renyuneyun@gmail.com>
*
* 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.plugins.operation.hotspot;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;

import ryey.easer.commons.IllegalXmlException;
import ryey.easer.plugins.operation.BooleanOperationData;

import static ryey.easer.plugins.operation.hotspot.HotspotOperationPlugin.pname;

public class HotspotOperationData extends BooleanOperationData {
public HotspotOperationData() {
}

public HotspotOperationData(Boolean state) {
super(state);
}

@Override
public void parse(XmlPullParser parser) throws IOException, XmlPullParserException, IllegalXmlException {
mParse(parser, pname());
}

@Override
public void serialize(XmlSerializer serializer) throws IOException {
mSerialize(serializer, pname());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2016 - 2017 Rui Zhao <renyuneyun@gmail.com>
*
* 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.plugins.operation.hotspot;

import android.content.Context;

import ryey.easer.commons.plugindef.ContentLayout;
import ryey.easer.commons.plugindef.operationplugin.OperationData;
import ryey.easer.commons.plugindef.operationplugin.OperationLoader;
import ryey.easer.commons.plugindef.operationplugin.OperationPlugin;

public class HotspotOperationPlugin implements OperationPlugin {
public static String pname() {
return "hotspot";
}

@Override
public String name() {
return pname();
}

@Override
public OperationData data() {
return new HotspotOperationData();
}

@Override
public ContentLayout view(Context context) {
return new HotspotContentLayout(context);
}

@Override
public OperationLoader loader(Context context) {
return new HotspotLoader(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public RingerModeLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
Integer mode = (Integer) data.get();
if (mode == null)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public RotationLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
ContentResolver resolver = context.getContentResolver();
Boolean state = (Boolean) data.get();
if (state == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public WifiLoader(Context context) {
}

@Override
public boolean load(OperationData data) {
public boolean _load(OperationData data) {
Boolean state = (Boolean) data.get();
if (state == null)
return true;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<string name="operation_wifi">WiFi</string>
<string name="operation_ringer_mode">響鈴模式</string>
<string name="operation_command">執行命令</string>
<string name="operation_hotspot">移動熱點</string>

<string-array name="event_type">
<item>晚於</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<string name="operation_brightness_desc_autobrightness">Auto brightness</string>
<string name="operation_ringer_mode">Ringer Mode</string>
<string name="operation_command">Run commands</string>
<string name="operation_hotspot">Hotspot</string>

<string-array name="event_type">
<item>After</item>
Expand Down

0 comments on commit f804065

Please # to comment.