Skip to content

Commit

Permalink
communication activity working
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyli committed Jul 22, 2016
1 parent 2c8d3e2 commit 3cae9fd
Show file tree
Hide file tree
Showing 16 changed files with 281 additions and 6 deletions.
Binary file modified .gradle/2.4/taskArtifacts/cache.properties.lock
Binary file not shown.
Binary file modified .gradle/2.4/taskArtifacts/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/2.4/taskArtifacts/fileSnapshots.bin
Binary file not shown.
Binary file modified .gradle/2.4/taskArtifacts/outputFileStates.bin
Binary file not shown.
Binary file modified .gradle/2.4/taskArtifacts/taskArtifacts.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<activity
android:name=".MainActivity"
android:label="Sugilite"
android:theme="@style/AppTheme.NoActionBar" >
android:theme="@style/AppTheme.NoActionBar"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down Expand Up @@ -61,8 +62,15 @@
</activity>
<activity
android:name=".mRecordingPopUpActivity"
android:label="Sugilite Script Recording"
>
android:noHistory="true"
android:label="Sugilite Script Recording" >
</activity>
<activity android:name=".communication.SugiliteCommunicationActicvity"
android:noHistory="true">
<intent-filter>
<action android:name="edu.cmu.hcii.sugilite.COMMUNICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/edu/cmu/hcii/sugilite/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.cmu.hcii.sugilite;

/**
* @author toby
* @date 7/22/16
* @time 12:18 PM
*/
public class RecordingPopUpDialog {
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ private Intent generatePopUpActivityIntentFromEvent(AccessibilityEvent event, Ac
//pop up the selection window
Intent popUpIntent = new Intent(this, mRecordingPopUpActivity.class);
popUpIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
popUpIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

popUpIntent.putExtra("packageName", sourceNode.getPackageName());
popUpIntent.putExtra("className", sourceNode.getClassName());
popUpIntent.putExtra("text", sourceNode.getText());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package edu.cmu.hcii.sugilite.communication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import edu.cmu.hcii.sugilite.R;
import edu.cmu.hcii.sugilite.SugiliteData;
import edu.cmu.hcii.sugilite.automation.ServiceStatusManager;
import edu.cmu.hcii.sugilite.dao.SugiliteScriptDao;
import edu.cmu.hcii.sugilite.model.block.SugiliteStartingBlock;
import edu.cmu.hcii.sugilite.ui.VariableSetValueDialog;

public class SugiliteCommunicationActicvity extends AppCompatActivity {
TextView messageType, scriptName;
SugiliteScriptDao sugiliteScriptDao;
SugiliteBlockJSONProcessor jsonProcessor;
SugiliteData sugiliteData;
SharedPreferences sharedPreferences;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sugilite_communication_acticvity);
messageType = (TextView)findViewById(R.id.receive_message_textview);
scriptName = (TextView)findViewById(R.id.receive_message_script_name);
messageType.setText("TEST MESSAGE TYPE");
String arg1 = "", arg2 = "";
if (getIntent().getExtras() != null)
{
arg1 = getIntent().getStringExtra("messageType");
messageType.setText(arg1);
/*
START_RECORDING, scriptName
END_RECORDING, "NULL"
RUN_SCRIPT, scriptName
GET_SCRIPT, scriptName
GET_SCRIPT_LIST, "NULL
*/

arg2 = getIntent().getStringExtra("scriptName");
scriptName.setText(arg2);

}
this.sugiliteScriptDao = new SugiliteScriptDao(this);
this.jsonProcessor = new SugiliteBlockJSONProcessor(this);
this.sugiliteData = (SugiliteData)getApplication();
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
this.context = this;
handleRequest(arg1, arg2);

}

private void handleRequest(String messageType, final String scriptName){
boolean recordingInProcess = sharedPreferences.getBoolean("recording_in_process", false);
switch (messageType){
case "START_RECORDING":
if(recordingInProcess) {
//the exception message below will be sent when there's already recording in process
//TODO: send exception message
}
else {
//NOTE: script name should be specified in msg.getData().getString("request");
if (scriptName != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("New Recording")
.setMessage("Now start recording new script " + scriptName)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sugiliteData.clearInstructionQueue();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("scriptName", scriptName);
editor.putBoolean("recording_in_process", true);
editor.commit();

sugiliteData.initiateScript(scriptName + ".SugiliteScript");
sugiliteData.initiatedExternally = true;

try {
sugiliteScriptDao.save(sugiliteData.getScriptHead());
} catch (Exception e) {
e.printStackTrace();
}

Toast.makeText(getApplicationContext(), "Recording new script " + sharedPreferences.getString("scriptName", "NULL"), Toast.LENGTH_SHORT).show();

//go to home screen for recording
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
}
break;
case "END_RECORDING":
if(recordingInProcess) {

SharedPreferences.Editor prefEditor = sharedPreferences.edit();
prefEditor.putBoolean("recording_in_process", false);
prefEditor.commit();
if(sugiliteData.initiatedExternally == true && sugiliteData.getScriptHead() != null)
sendRecordingFinishedSignal(sugiliteData.getScriptHead().getScriptName());

Toast.makeText(context, "end recording", Toast.LENGTH_SHORT).show();
}
else {
//the exception message below will be sent when there's no recording in process
//TODO: send exception message
}
break;
case "RUN_SCRIPT":
if(recordingInProcess) {
//TODO: send exception message
}
else {
SugiliteStartingBlock script = sugiliteScriptDao.read(scriptName + ".SugiliteScript");
if(script == null)
//TODO: send exception message
sugiliteData.clearInstructionQueue();
final ServiceStatusManager serviceStatusManager = new ServiceStatusManager(context);

if(!serviceStatusManager.isRunning()){
//prompt the user if the accessiblity service is not active
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setTitle("Service not running")
.setMessage("The Sugilite accessiblity service is not enabled. Please enable the service in the phone settings before recording.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
serviceStatusManager.promptEnabling();
//do nothing
}
});
AlertDialog dialog = builder1.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
else {
sugiliteData.stringVariableMap.putAll(script.variableNameDefaultValueMap);

//kill all the relevant packages
for (String packageName : script.relevantPackages) {
try {
Process sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
os.write(("am force-stop " + packageName).getBytes("ASCII"));
os.flush();
os.close();
System.out.println(packageName);
} catch (Exception e) {
e.printStackTrace();
// do nothing, likely this exception is caused by non-rooted device
}
}
sugiliteData.runScript(script);
try {
Thread.sleep(VariableSetValueDialog.SCRIPT_DELAY);
} catch (Exception e) {
// do nothing
}
//go to home screen for running the automation
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(startMain);
}
}
break;
case "GET_SCRIPT":
SugiliteStartingBlock script = sugiliteScriptDao.read(scriptName + ".SugiliteScript");
if(script != null)
sendReturnValue(jsonProcessor.scriptToJson(script));
else
//the exception message below will be sent when can't find a script with provided name
//TODO: send exception message
break;
case "GET_SCRIPT_LIST":
List<String> allNames = sugiliteScriptDao.getAllNames();
List<String> retVal = new ArrayList<>();
for(String name : allNames)
retVal.add(name.replace(".SugiliteScript", ""));
sendReturnValue(new Gson().toJson(retVal));
break;
}
}

public boolean sendRecordingFinishedSignal(String scriptName){
return true;
}

private void sendReturnValue(String retVal){
Intent returnIntent = new Intent();
returnIntent.putExtra("result", retVal);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ public void onClick(DialogInterface dialog, int which) {
context.startActivity(startMain);
}
}
break;

default:
Log.e( TAG, "Message not supported!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import edu.cmu.hcii.sugilite.model.block.SugiliteOperationBlock;
Expand All @@ -16,9 +17,10 @@ public class SugiliteScriptJSON {
public SugiliteScriptJSON(SugiliteStartingBlock startingBlock){
this.scriptName = new String(startingBlock.getScriptName());
this.scriptName.replace(".SugiliteScript", "");
variables = new HashMap<>();
for(Map.Entry<String, Variable> entry : startingBlock.variableNameDefaultValueMap.entrySet()){
if(entry.getValue() instanceof StringVariable)
variables.put(entry.getKey(), ((StringVariable) entry.getValue()).getValue());
this.variables.put(entry.getKey(), ((StringVariable) entry.getValue()).getValue());
}
if(startingBlock.getNextBlock() != null && startingBlock.getNextBlock() instanceof SugiliteOperationBlock)
nextBlock = new SugiliteOperationBlockJSON((SugiliteOperationBlock)startingBlock.getNextBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void setupSelections(){
SimpleDateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
boolean autoFillEnabled = sharedPreferences.getBoolean("auto_fill_enabled", false);
boolean autoFillEnabled = sharedPreferences.getBoolean("auto_fill_enabled", true);
LinearLayout identifierLayout = (LinearLayout) findViewById(R.id.identifier_layout);

CompoundButton.OnCheckedChangeListener identiferCheckboxChangeListener = new CompoundButton.OnCheckedChangeListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="edu.cmu.hcii.sugilite.communication.SugiliteCommunicationActicvity">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NULL"
android:id="@+id/receive_message_textview" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NULL"
android:id="@+id/receive_message_script_name" />
</LinearLayout>
</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@
<string name="pref_ringtone_silent">Silent</string>

<string name="pref_title_vibrate">Vibrate</string>
<string name="title_activity_communication_interface">CommunicationInterfaceActivity</string>

</resources>

0 comments on commit 3cae9fd

Please sign in to comment.