2021-03-26 19:58:27 +01:00
|
|
|
package com.jens.automation2;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
2021-03-27 19:57:39 +01:00
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.CompoundButton;
|
2021-03-26 19:58:27 +01:00
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.Spinner;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2021-03-28 20:33:44 +02:00
|
|
|
import static com.jens.automation2.Trigger.triggerParameter2Split;
|
|
|
|
|
2021-03-27 14:11:39 +01:00
|
|
|
public class ActivityManageTriggerNotification extends Activity
|
2021-03-26 19:58:27 +01:00
|
|
|
{
|
2022-01-09 21:43:49 +01:00
|
|
|
public static final String intentNameNotificationApp = "app";
|
|
|
|
public static final String intentNameNotificationTitleDir = "titleDir";
|
|
|
|
public static final String intentNameNotificationTitle = "title";
|
|
|
|
public static final String intentNameNotificationTextDir = "textDir";
|
|
|
|
public static final String intentNameNotificationText = "text";
|
|
|
|
public static final String intentNameNotificationDirection = "direction";
|
|
|
|
|
2021-03-28 20:33:44 +02:00
|
|
|
public static Trigger editedNotificationTrigger;
|
2021-05-13 23:50:14 +02:00
|
|
|
boolean edit = false;
|
|
|
|
ProgressDialog progressDialog = null;
|
|
|
|
|
2021-03-26 23:11:36 +01:00
|
|
|
EditText etNotificationTitle, etNotificationText;
|
|
|
|
Button bSelectApp, bSaveTriggerNotification;
|
|
|
|
Spinner spinnerTitleDirection, spinnerTextDirection;
|
2021-03-27 19:57:39 +01:00
|
|
|
TextView tvSelectedApplication;
|
|
|
|
CheckBox chkNotificationDirection;
|
2021-03-26 19:58:27 +01:00
|
|
|
|
|
|
|
private static List<PackageInfo> pInfos = null;
|
2021-03-27 19:57:39 +01:00
|
|
|
public static Trigger resultingTrigger;
|
2021-03-26 19:58:27 +01:00
|
|
|
|
2021-03-26 23:11:36 +01:00
|
|
|
private static String[] directions;
|
2021-03-26 19:58:27 +01:00
|
|
|
|
2021-03-26 23:11:36 +01:00
|
|
|
ArrayAdapter<String> directionSpinnerAdapter;
|
2021-03-26 19:58:27 +01:00
|
|
|
|
|
|
|
public static void getActivityList(final Context context)
|
|
|
|
{
|
|
|
|
if(pInfos == null)
|
|
|
|
{
|
|
|
|
pInfos = context.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
|
|
|
|
Collections.sort(pInfos, new Comparator<PackageInfo>()
|
|
|
|
{
|
|
|
|
public int compare(PackageInfo obj1, PackageInfo obj2)
|
|
|
|
{
|
|
|
|
String name1 = "";
|
|
|
|
String name2 = "";
|
2021-03-26 23:11:36 +01:00
|
|
|
|
2021-03-26 19:58:27 +01:00
|
|
|
ApplicationInfo aInfo1 = obj1.applicationInfo;
|
|
|
|
if (aInfo1 != null)
|
|
|
|
{
|
|
|
|
name1 = (String) context.getPackageManager().getApplicationLabel(aInfo1);
|
|
|
|
}
|
|
|
|
ApplicationInfo aInfo2 = obj2.applicationInfo;
|
|
|
|
if (aInfo2 != null)
|
|
|
|
{
|
|
|
|
name2 = (String) context.getPackageManager().getApplicationLabel(aInfo2);
|
|
|
|
}
|
2021-03-26 23:11:36 +01:00
|
|
|
|
2021-03-26 19:58:27 +01:00
|
|
|
return name1.compareTo(name2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] getApplicationNameListString(Context myContext)
|
|
|
|
{
|
|
|
|
// Generate the actual list
|
|
|
|
getActivityList(myContext);
|
|
|
|
|
|
|
|
ArrayList<String> returnList = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (PackageInfo pInfo : pInfos)
|
|
|
|
{
|
|
|
|
ApplicationInfo aInfo = pInfo.applicationInfo;
|
|
|
|
if (aInfo != null)
|
|
|
|
{
|
|
|
|
String aLabel;
|
|
|
|
|
2021-03-26 23:11:36 +01:00
|
|
|
aLabel = (String) myContext.getPackageManager().getApplicationLabel(aInfo);
|
2021-03-26 19:58:27 +01:00
|
|
|
|
|
|
|
ActivityInfo[] aInfos = pInfo.activities;
|
|
|
|
if (aInfos != null && aInfos.length > 0) // Only put Applications into the list that have packages.
|
|
|
|
{
|
|
|
|
if(!returnList.contains(aLabel))
|
|
|
|
returnList.add(aLabel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnList.toArray(new String[returnList.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] getPackageListString(Context myContext, String applicationLabel)
|
|
|
|
{
|
|
|
|
// Generate the actual list
|
|
|
|
getActivityList(myContext);
|
|
|
|
|
|
|
|
ArrayList<String> returnList = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (PackageInfo pInfo : pInfos)
|
|
|
|
{
|
|
|
|
if(myContext.getPackageManager().getApplicationLabel(pInfo.applicationInfo).equals(applicationLabel))
|
|
|
|
{
|
|
|
|
ActivityInfo[] aInfos = pInfo.activities;
|
|
|
|
if (aInfos != null && aInfos.length > 0)
|
|
|
|
{
|
|
|
|
returnList.add(pInfo.packageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnList.toArray(new String[returnList.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] getPackageListString(Context myContext)
|
|
|
|
{
|
|
|
|
// Generate the actual list
|
|
|
|
getActivityList(myContext);
|
|
|
|
|
|
|
|
ArrayList<String> returnList = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (PackageInfo pInfo : pInfos)
|
|
|
|
{
|
|
|
|
ActivityInfo[] aInfos = pInfo.activities;
|
|
|
|
if (aInfos != null && aInfos.length > 0)
|
|
|
|
{
|
|
|
|
returnList.add(pInfo.packageName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Miscellaneous.logEvent("w", "Empty Application", "Application " + myContext.getPackageManager().getApplicationLabel(pInfo.applicationInfo) + " doesn\'t have packages.", 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnList.toArray(new String[returnList.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] getActivityListForPackageName(String packageName)
|
|
|
|
{
|
|
|
|
ArrayList<String> returnList = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (PackageInfo pInfo : pInfos)
|
|
|
|
{
|
|
|
|
if(pInfo.packageName.equals(packageName))
|
|
|
|
{
|
|
|
|
ActivityInfo[] aInfos = pInfo.activities;
|
|
|
|
if (aInfos != null)
|
|
|
|
{
|
|
|
|
for (ActivityInfo activityInfo : aInfos)
|
|
|
|
{
|
|
|
|
returnList.add(activityInfo.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnList.toArray(new String[returnList.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ActivityInfo getActivityInfoForPackageNameAndActivityName(String packageName, String activityName)
|
|
|
|
{
|
|
|
|
for (PackageInfo pInfo : pInfos)
|
|
|
|
{
|
|
|
|
if(pInfo.packageName.equals(packageName))
|
|
|
|
{
|
|
|
|
ActivityInfo[] aInfos = pInfo.activities;
|
|
|
|
if (aInfos != null)
|
|
|
|
{
|
|
|
|
for (ActivityInfo activityInfo : aInfos)
|
|
|
|
{
|
|
|
|
if(activityInfo.name.equals(activityName))
|
|
|
|
return activityInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private AlertDialog getActionStartActivityDialog1()
|
|
|
|
{
|
|
|
|
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
alertDialogBuilder.setTitle(getResources().getString(R.string.selectApplication));
|
2021-03-27 14:11:39 +01:00
|
|
|
final String[] applicationArray = ActivityManageTriggerNotification.getApplicationNameListString(this);
|
2021-03-26 19:58:27 +01:00
|
|
|
alertDialogBuilder.setItems(applicationArray, new DialogInterface.OnClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which)
|
|
|
|
{
|
|
|
|
dialog.dismiss();
|
|
|
|
getActionStartActivityDialog2(applicationArray[which]).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
AlertDialog alertDialog = alertDialogBuilder.create();
|
|
|
|
|
|
|
|
return alertDialog;
|
|
|
|
}
|
|
|
|
private AlertDialog getActionStartActivityDialog2(String applicationName)
|
|
|
|
{
|
|
|
|
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
alertDialogBuilder.setTitle(getResources().getString(R.string.selectPackageOfApplication));
|
2021-03-27 14:11:39 +01:00
|
|
|
final String[] packageArray = ActivityManageTriggerNotification.getPackageListString(this, applicationName);
|
2021-03-26 19:58:27 +01:00
|
|
|
alertDialogBuilder.setItems(packageArray, new DialogInterface.OnClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which)
|
|
|
|
{
|
2021-03-26 23:11:36 +01:00
|
|
|
//getActionStartActivityDialog3(packageArray[which]).show();
|
|
|
|
//Miscellaneous.messageBox(getResources().getString(R.string.hint), getResources().getString(R.string.chooseActivityHint), ActivityManageNotificationTrigger.this).show();
|
2021-03-27 19:57:39 +01:00
|
|
|
tvSelectedApplication.setText(packageArray[which]);
|
2021-03-26 19:58:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
AlertDialog alertDialog = alertDialogBuilder.create();
|
|
|
|
|
|
|
|
return alertDialog;
|
|
|
|
}
|
|
|
|
private AlertDialog getActionStartActivityDialog3(final String packageName)
|
|
|
|
{
|
|
|
|
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
alertDialogBuilder.setTitle(getResources().getString(R.string.selectActivityToBeStarted));
|
2021-03-27 14:11:39 +01:00
|
|
|
final String activityArray[] = ActivityManageTriggerNotification.getActivityListForPackageName(packageName);
|
2021-03-26 19:58:27 +01:00
|
|
|
alertDialogBuilder.setItems(activityArray, new DialogInterface.OnClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which)
|
|
|
|
{
|
2021-03-27 14:11:39 +01:00
|
|
|
ActivityInfo ai = ActivityManageTriggerNotification.getActivityInfoForPackageNameAndActivityName(packageName, activityArray[which]);
|
2021-03-27 19:57:39 +01:00
|
|
|
tvSelectedApplication.setText(ai.packageName + ";" + ai.name);
|
2021-03-26 19:58:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
AlertDialog alertDialog = alertDialogBuilder.create();
|
|
|
|
|
|
|
|
return alertDialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
2021-05-13 18:37:34 +02:00
|
|
|
setContentView(R.layout.activity_manage_trigger_notification);
|
2021-03-26 23:11:36 +01:00
|
|
|
|
|
|
|
etNotificationTitle = (EditText)findViewById(R.id.etNotificationTitle);
|
|
|
|
etNotificationText = (EditText)findViewById(R.id.etNotificationText);
|
2021-03-26 19:58:27 +01:00
|
|
|
bSelectApp = (Button)findViewById(R.id.bSelectApp);
|
2022-01-11 16:04:04 +01:00
|
|
|
bSaveTriggerNotification = (Button)findViewById(R.id.bSaveActionCloseNotification);
|
2021-03-26 23:11:36 +01:00
|
|
|
spinnerTitleDirection = (Spinner)findViewById(R.id.spinnerTitleDirection);
|
|
|
|
spinnerTextDirection = (Spinner)findViewById(R.id.spinnerTextDirection);
|
2021-05-09 14:42:24 +02:00
|
|
|
tvSelectedApplication = (TextView)findViewById(R.id.etActivityOrActionPath);
|
2021-03-27 19:57:39 +01:00
|
|
|
chkNotificationDirection = (CheckBox)findViewById(R.id.chkNotificationDirection);
|
2021-03-26 23:11:36 +01:00
|
|
|
|
|
|
|
directions = new String[] {
|
|
|
|
getResources().getString(R.string.directionStringEquals),
|
|
|
|
getResources().getString(R.string.directionStringContains),
|
2021-03-27 14:11:39 +01:00
|
|
|
getResources().getString(R.string.directionStringStartsWith),
|
2021-03-26 23:11:36 +01:00
|
|
|
getResources().getString(R.string.directionStringEndsWith),
|
|
|
|
getResources().getString(R.string.directionStringNotEquals)
|
|
|
|
};
|
|
|
|
|
2021-03-27 14:11:39 +01:00
|
|
|
directionSpinnerAdapter = new ArrayAdapter<String>(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageTriggerNotification.directions);
|
2021-03-26 23:11:36 +01:00
|
|
|
spinnerTitleDirection.setAdapter(directionSpinnerAdapter);
|
|
|
|
spinnerTextDirection.setAdapter(directionSpinnerAdapter);
|
|
|
|
directionSpinnerAdapter.notifyDataSetChanged();
|
2021-03-26 19:58:27 +01:00
|
|
|
|
|
|
|
bSelectApp.setOnClickListener(new OnClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
|
|
|
GetActivityListTask getActivityListTask = new GetActivityListTask();
|
|
|
|
getActivityListTask.execute();
|
2021-03-27 14:11:39 +01:00
|
|
|
progressDialog = ProgressDialog.show(ActivityManageTriggerNotification.this, "", ActivityManageTriggerNotification.this.getResources().getString(R.string.gettingListOfInstalledApplications));
|
2021-03-26 19:58:27 +01:00
|
|
|
}
|
|
|
|
});
|
2021-03-27 19:57:39 +01:00
|
|
|
|
|
|
|
chkNotificationDirection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
|
|
|
|
{
|
|
|
|
if(isChecked)
|
|
|
|
chkNotificationDirection.setText(getResources().getString(R.string.notificationAppears));
|
|
|
|
else
|
|
|
|
chkNotificationDirection.setText(getResources().getString(R.string.notificationDisappears));
|
|
|
|
}
|
|
|
|
});
|
2021-03-26 19:58:27 +01:00
|
|
|
|
2021-03-26 23:11:36 +01:00
|
|
|
bSaveTriggerNotification.setOnClickListener(new OnClickListener()
|
2021-03-26 19:58:27 +01:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
2021-03-27 19:57:39 +01:00
|
|
|
String app;
|
|
|
|
if(tvSelectedApplication.getText().toString().equalsIgnoreCase(getResources().getString(R.string.anyApp)))
|
|
|
|
app = "-1";
|
|
|
|
else
|
|
|
|
app = tvSelectedApplication.getText().toString();
|
2021-03-27 14:11:39 +01:00
|
|
|
|
2021-03-27 19:57:39 +01:00
|
|
|
String titleDir = Trigger.getMatchCode(spinnerTitleDirection.getSelectedItem().toString());
|
|
|
|
String title = etNotificationTitle.getText().toString();
|
|
|
|
String textDir = Trigger.getMatchCode(spinnerTextDirection.getSelectedItem().toString());
|
|
|
|
String text = etNotificationText.getText().toString();
|
2021-03-27 14:11:39 +01:00
|
|
|
|
2021-03-28 20:33:44 +02:00
|
|
|
if(edit)
|
|
|
|
{
|
|
|
|
editedNotificationTrigger.setTriggerParameter(chkNotificationDirection.isChecked());
|
|
|
|
editedNotificationTrigger.setTriggerParameter2(app + triggerParameter2Split + titleDir + triggerParameter2Split + title + triggerParameter2Split + textDir + triggerParameter2Split + text);
|
|
|
|
ActivityManageTriggerNotification.this.setResult(RESULT_OK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Intent data = new Intent();
|
2022-01-09 21:43:49 +01:00
|
|
|
data.putExtra(intentNameNotificationDirection, chkNotificationDirection.isChecked());
|
|
|
|
data.putExtra(intentNameNotificationApp, app);
|
|
|
|
data.putExtra(intentNameNotificationTitleDir, titleDir);
|
|
|
|
data.putExtra(intentNameNotificationTitle, title);
|
|
|
|
data.putExtra(intentNameNotificationTextDir, textDir);
|
|
|
|
data.putExtra(intentNameNotificationText, text);
|
2021-03-28 20:33:44 +02:00
|
|
|
ActivityManageTriggerNotification.this.setResult(RESULT_OK, data);
|
|
|
|
}
|
2021-03-27 14:11:39 +01:00
|
|
|
|
2021-03-27 19:57:39 +01:00
|
|
|
finish();
|
2021-03-26 19:58:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Intent i = getIntent();
|
|
|
|
if(i.getBooleanExtra("edit", false) == true)
|
|
|
|
{
|
|
|
|
edit = true;
|
|
|
|
loadValuesIntoGui();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadValuesIntoGui()
|
|
|
|
{
|
2021-03-28 20:33:44 +02:00
|
|
|
chkNotificationDirection.setChecked(editedNotificationTrigger.getTriggerParameter());
|
|
|
|
|
|
|
|
String[] params = editedNotificationTrigger.getTriggerParameter2().split(triggerParameter2Split);
|
|
|
|
|
|
|
|
String app = params[0];
|
|
|
|
String titleDir = params[1];
|
|
|
|
String title = params[2];
|
|
|
|
String textDir = params[3];
|
|
|
|
String text;
|
|
|
|
if (params.length >= 5)
|
|
|
|
text = params[4];
|
|
|
|
else
|
|
|
|
text = "";
|
|
|
|
|
|
|
|
if(!app.equals("-1"))
|
|
|
|
tvSelectedApplication.setText(app);
|
|
|
|
|
|
|
|
for(int i = 0; i < directions.length; i++)
|
|
|
|
{
|
|
|
|
if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(titleDir))
|
|
|
|
spinnerTitleDirection.setSelection(i);
|
|
|
|
|
|
|
|
if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(textDir))
|
|
|
|
spinnerTextDirection.setSelection(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(title.length() > 0)
|
|
|
|
etNotificationTitle.setText(title);
|
|
|
|
|
|
|
|
if(text.length() > 0)
|
|
|
|
etNotificationText.setText(text);
|
2021-03-26 19:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class GetActivityListTask extends AsyncTask<Void, Void, Void>
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params)
|
|
|
|
{
|
2021-03-27 14:11:39 +01:00
|
|
|
getActivityList(ActivityManageTriggerNotification.this);
|
2021-03-26 19:58:27 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result)
|
|
|
|
{
|
|
|
|
progressDialog.dismiss();
|
|
|
|
getActionStartActivityDialog1().show();
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 21:43:49 +01:00
|
|
|
}
|