diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 1202b882..ab5baa87 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -930,7 +930,16 @@ public class Rule implements Comparable publishProgress(message); for(int i = 0; i< Rule.this.getActionSet().size(); i++) - Rule.this.getActionSet().get(i).run(automationService, doToggle); + { + try + { + Rule.this.getActionSet().get(i).run(automationService, doToggle); + } + catch(Exception e) + { + Miscellaneous.logEvent("e", "RuleExecution", "Error running action of rule " + Rule.this.getName() + ": " + Log.getStackTraceString(e), 1); + } + } // Keep log of last x rule activations (Settings) try diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index eb6c1f2f..0f957014 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -2,7 +2,9 @@ package com.jens.automation2; import android.content.Context; import android.os.AsyncTask; +import android.text.style.TabStopSpan; import android.util.Log; +import android.widget.Toast; import org.apache.http.client.methods.HttpGet; @@ -329,14 +331,16 @@ public class Action } public void run(Context context, boolean toggleActionIfPossible) - { - switch(this.getAction()) + { + try { - case changeSoundProfile: - /* - * Old version. Those checks should not be necessary anymore. Also they didn't work - * because profiles were created with names like silent, vibrate and normal. - */ + switch(this.getAction()) + { + case changeSoundProfile: + /* + * Old version. Those checks should not be necessary anymore. Also they didn't work + * because profiles were created with names like silent, vibrate and normal. + */ // if(this.getParameter2().equals("silent")) // Actions.setSound(context, AudioManager.RINGER_MODE_SILENT); // else if(this.getParameter2().equals("vibrate")) @@ -346,70 +350,76 @@ public class Action // else // { Profile p = Profile.getByName(this.getParameter2()); - if(p != null) + if (p != null) p.activate(context); // } - break; - case triggerUrl: - triggerUrl(context); - break; - case setBluetooth: - Actions.setBluetooth(context, getParameter1(), toggleActionIfPossible); - break; - case setUsbTethering: - Actions.setUsbTethering(context, getParameter1(), toggleActionIfPossible); - break; - case setWifi: - Actions.setWifi(context, getParameter1(), toggleActionIfPossible); - break; - case setWifiTethering: - Actions.setWifiTethering(context, getParameter1(), toggleActionIfPossible); - break; - case setDisplayRotation: - Actions.setDisplayRotation(context, getParameter1(), toggleActionIfPossible); - break; - case startOtherActivity: - Actions.startOtherActivity(getParameter2()); - break; - case waitBeforeNextAction: - Actions.waitBeforeNextAction(Long.parseLong(this.getParameter2())); - break; - case wakeupDevice: - Actions.wakeupDevice(Long.parseLong(this.getParameter2())); - // wakeupDevice() will create a seperate thread. That'll take some time, we wait 100ms. - try - { - Thread.sleep(100); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - break; - case setAirplaneMode: - Actions.setAirplaneMode(this.getParameter1(), toggleActionIfPossible); - break; - case setDataConnection: - Actions.MobileDataStuff.setDataConnection(this.getParameter1(), toggleActionIfPossible); - break; - case speakText: - Actions.speakText(this.getParameter2()); - break; - case playMusic: - Actions.playMusic(this.getParameter1(), toggleActionIfPossible); - break; - case sendTextMessage: - Actions.sendTextMessage(context, this.getParameter2().split(Actions.smsSeparator)); - break; - case setScreenBrightness: - Actions.setScreenBrightness(getParameter1(), Integer.parseInt(getParameter2())); - break; - case playSound: - Actions.playSound(getParameter1(), getParameter2()); - break; - default: - Miscellaneous.logEvent("w", "Action", context.getResources().getString(R.string.unknownActionSpecified), 3); - break; + break; + case triggerUrl: + triggerUrl(context); + break; + case setBluetooth: + Actions.setBluetooth(context, getParameter1(), toggleActionIfPossible); + break; + case setUsbTethering: + Actions.setUsbTethering(context, getParameter1(), toggleActionIfPossible); + break; + case setWifi: + Actions.setWifi(context, getParameter1(), toggleActionIfPossible); + break; + case setWifiTethering: + Actions.setWifiTethering(context, getParameter1(), toggleActionIfPossible); + break; + case setDisplayRotation: + Actions.setDisplayRotation(context, getParameter1(), toggleActionIfPossible); + break; + case startOtherActivity: + Actions.startOtherActivity(getParameter2()); + break; + case waitBeforeNextAction: + Actions.waitBeforeNextAction(Long.parseLong(this.getParameter2())); + break; + case wakeupDevice: + Actions.wakeupDevice(Long.parseLong(this.getParameter2())); + // wakeupDevice() will create a seperate thread. That'll take some time, we wait 100ms. + try + { + Thread.sleep(100); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + break; + case setAirplaneMode: + Actions.setAirplaneMode(this.getParameter1(), toggleActionIfPossible); + break; + case setDataConnection: + Actions.MobileDataStuff.setDataConnection(this.getParameter1(), toggleActionIfPossible); + break; + case speakText: + Actions.speakText(this.getParameter2()); + break; + case playMusic: + Actions.playMusic(this.getParameter1(), toggleActionIfPossible); + break; + case sendTextMessage: + Actions.sendTextMessage(context, this.getParameter2().split(Actions.smsSeparator)); + break; + case setScreenBrightness: + Actions.setScreenBrightness(getParameter1(), Integer.parseInt(getParameter2())); + break; + case playSound: + Actions.playSound(getParameter1(), getParameter2()); + break; + default: + Miscellaneous.logEvent("w", "Action", context.getResources().getString(R.string.unknownActionSpecified), 3); + break; + } + } + catch(Exception e) + { + Miscellaneous.logEvent("e", "ActionExecution", Log.getStackTraceString(e), 1); + Toast.makeText(context, context.getResources().getString(R.string.errorRunningRule), Toast.LENGTH_LONG).show(); } } diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index fba5ce6d..a489c2e5 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -36,6 +36,7 @@ import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.util.InetAddressUtils; import org.apache.http.impl.client.DefaultHttpClient; +import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.InetAddress; @@ -476,19 +477,28 @@ public class Actions MediaPlayer mp = new MediaPlayer(); try { - Uri fileUri = Uri.parse(soundFileLocation); - mp.setLooping(false); - mp.setDataSource(Miscellaneous.getAnyContext(), fileUri); - mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() + File file = new File(soundFileLocation); + if(file.exists()) { - @Override - public void onCompletion(MediaPlayer mp) + Uri fileUri = Uri.parse(soundFileLocation); + mp.setLooping(false); + mp.setDataSource(Miscellaneous.getAnyContext(), fileUri); + mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { - mp.release(); - } - }); - mp.prepare(); - mp.start(); + @Override + public void onCompletion(MediaPlayer mp) + { + mp.release(); + } + }); + mp.prepare(); + mp.start(); + } + else + { + Miscellaneous.logEvent("w", "Play sound file", "Sound file " + soundFileLocation + " does not exist. Can't play it.", 2); + Toast.makeText(context, String.format(context.getResources().getString(R.string.cantFindSoundFile), soundFileLocation), Toast.LENGTH_SHORT).show(); + } } catch (Exception e) { @@ -561,7 +571,9 @@ public class Actions Miscellaneous.logEvent("i", "StartOtherActivity", "Starting other Activity...", 4); String packageName, className; + String params[] = param.split(";"); + packageName = params[0]; className = params[1]; @@ -571,7 +583,11 @@ public class Actions Intent externalActivityIntent = new Intent(Intent.ACTION_MAIN); externalActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); externalActivityIntent.addCategory(Intent.CATEGORY_LAUNCHER); - externalActivityIntent.setClassName(packageName, className); + + if(packageName.equals("dummyPkg")) + externalActivityIntent.setAction(className); + else + externalActivityIntent.setClassName(packageName, className); // has intent values to deliver for(int i=2; i(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageActionStartActivity.supportedIntentTypes); spinnerParameterType.setAdapter(intentTypeSpinnerAdapter); intentTypeSpinnerAdapter.notifyDataSetChanged(); - intentPairAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_smalltextsize, intentPairList); + intentPairAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, intentPairList); bSelectApp.setOnClickListener(new OnClickListener() { @@ -408,7 +407,7 @@ public class ActivityManageActionStartActivity extends Activity String[] params = resultingAction.getParameter2().split(";"); if(params.length >= 2) { - tvSelectedActivity.setText(params[0] + ";" + params[1]); + etSelectedActivity.setText(params[0] + ";" + params[1]); if(params.length > 2) { @@ -434,13 +433,23 @@ public class ActivityManageActionStartActivity extends Activity private boolean saveAction() { - if(tvSelectedActivity.getText().toString().length() == 0) + if(etSelectedActivity.getText().toString().length() == 0) { Toast.makeText(ActivityManageActionStartActivity.this, getResources().getString(R.string.selectApplication), Toast.LENGTH_LONG).show(); return false; } +// else +// { +// Intent testIntent = new Intent(ActivityManageActionStartActivity.this, etSelectedActivity); +// Intent externalActivityIntent = new Intent(Intent.ACTION_MAIN); +// externalActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); +// externalActivityIntent.addCategory(Intent.CATEGORY_LAUNCHER); +// externalActivityIntent.setClassName(packageName, className); +// +// boolean activityExists = externalActivityIntent.resolveActivityInfo(getPackageManager(), 0) != null; +// } - if(tvSelectedActivity.getText().toString().equals(getResources().getString(R.string.selectApplication))) + if(etSelectedActivity.getText().toString().equals(getResources().getString(R.string.selectApplication))) { Toast.makeText(this, getResources().getString(R.string.selectApplication), Toast.LENGTH_LONG).show(); return false; @@ -451,7 +460,13 @@ public class ActivityManageActionStartActivity extends Activity resultingAction.setAction(Action_Enum.startOtherActivity); - String parameter2 = tvSelectedActivity.getText().toString(); + String parameter2; + + if(etSelectedActivity.getText().toString().contains(";")) + parameter2 = etSelectedActivity.getText().toString(); + else + parameter2 = "dummyPkg;" + etSelectedActivity.getText().toString(); + for(String s : intentPairList) parameter2 += ";" + s; diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java index d25ce28c..79b1311f 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java @@ -22,8 +22,6 @@ import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; -import com.jens.automation2.Action.Action_Enum; - import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -260,7 +258,7 @@ public class ActivityManageTriggerNotification extends Activity bSaveTriggerNotification = (Button)findViewById(R.id.bSaveTriggerNotification); spinnerTitleDirection = (Spinner)findViewById(R.id.spinnerTitleDirection); spinnerTextDirection = (Spinner)findViewById(R.id.spinnerTextDirection); - tvSelectedApplication = (TextView)findViewById(R.id.tvSelectedApplication); + tvSelectedApplication = (TextView)findViewById(R.id.etSelectedApplication); chkNotificationDirection = (CheckBox)findViewById(R.id.chkNotificationDirection); directions = new String[] { diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index a7b18e9a..cbf27490 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -403,7 +403,7 @@ public class ActivityPermissions extends Activity addToArrayListUnique("android.permission.ACCESS_NETWORK_STATE", requiredPermissions); break; case batteryLevel: - addToArrayListUnique("android.permission.READ_PHONE_STATE", requiredPermissions); +// addToArrayListUnique("android.permission.READ_PHONE_STATE", requiredPermissions); // addToArrayListUnique("android.permission.BATTERY_STATS", requiredPermissions); break; case bluetoothConnection: diff --git a/app/src/main/res/layout/action_start_activity.xml b/app/src/main/res/layout/action_start_activity.xml index 8647c12b..9404e222 100644 --- a/app/src/main/res/layout/action_start_activity.xml +++ b/app/src/main/res/layout/action_start_activity.xml @@ -1,117 +1,138 @@ + android:layout_height="wrap_content" + android:layout_margin="@dimen/default_margin" > - - - -