-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from tobyli/popup_recording
communication activity working
- Loading branch information
Showing
16 changed files
with
281 additions
and
6 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/edu/cmu/hcii/sugilite/RecordingPopUpDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
app/src/main/java/edu/cmu/hcii/sugilite/communication/SugiliteCommunicationActicvity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/src/main/res/layout/activity_sugilite_communication_acticvity.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters