Automation/app/src/main/java/com/jens/automation2/ActivityMainRules.java

264 lines
7.5 KiB
Java
Raw Normal View History

2021-02-16 13:42:49 +01:00
package com.jens.automation2;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.jens.automation2.AutomationService.serviceCommands;
2021-11-07 17:29:00 +01:00
import com.jens.automation2.receivers.DateTimeListener;
2021-02-16 13:42:49 +01:00
import java.util.ArrayList;
public class ActivityMainRules extends ActivityGeneric
{
private ListView ruleListView;
2021-06-20 22:24:17 +02:00
ArrayList<Rule> ruleList = new ArrayList<>();
2021-02-16 13:42:49 +01:00
private ArrayAdapter<Rule> ruleListViewAdapter;
public static Rule ruleToEdit;
protected static ActivityMainRules instance = null;
2021-06-20 22:24:17 +02:00
public static final int requestCodeCreateRule = 3000;
public static final int requestCodeChangeRule = 4000;
2021-02-16 13:42:49 +01:00
public static ActivityMainRules getInstance()
{
if(instance == null)
instance = new ActivityMainRules();
return instance;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_rule_layout);
instance = this;
Button bAddRule = (Button)findViewById(R.id.bAddRule);
bAddRule.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
ruleToEdit = null;
2021-02-17 22:27:53 +01:00
Intent startAddRuleIntent = new Intent(ActivityMainRules.this, ActivityManageRule.class);
2021-06-20 22:24:17 +02:00
startActivityForResult(startAddRuleIntent, requestCodeCreateRule);
2021-02-16 13:42:49 +01:00
}
});
2021-06-20 22:24:17 +02:00
ruleListViewAdapter = new RuleArrayAdapter(this, R.layout.view_for_rule_listview, ruleList);
2021-02-16 13:42:49 +01:00
ruleListView = (ListView)findViewById(R.id.lvRuleList);
ruleListView.setClickable(true);
ruleListView.setOnItemLongClickListener(new OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
getRuleDialog((Rule)ruleListView.getItemAtPosition(arg2)).show();
return false;
}
});
if(Settings.executeRulesAndProfilesWithSingleClick)
{
ruleListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(AutomationService.isMyServiceRunning(ActivityMainRules.this))
{
AutomationService runContext = AutomationService.getInstance();
if(runContext != null)
{
Rule rule = (Rule)ruleListView.getItemAtPosition(position);
rule.activate(runContext, true);
}
}
}
});
}
updateListView();
this.storeServiceReferenceInVariable();
}
private static class RuleHolder
{
public ImageView ivActiveInactive;
public TextView tvRuleName;
}
private static class RuleArrayAdapter extends ArrayAdapter<Rule>
{
public RuleArrayAdapter(Context context, int resource, ArrayList<Rule> objects)
{
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
RuleHolder holder = new RuleHolder();
// First let's verify the convertView is not null
if (convertView == null)
{
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.view_for_rule_listview, null);
// Now we can fill the layout with the right values
TextView tv = (TextView) v.findViewById(R.id.tvRuleName);
ImageView img = (ImageView) v.findViewById(R.id.ivActiveInactive);
holder.tvRuleName = tv;
holder.ivActiveInactive = img;
v.setTag(holder);
}
else
holder = (RuleHolder) v.getTag();
System.out.println("Position ["+position+"]");
Rule r = Rule.getRuleCollection().get(position);
holder.tvRuleName.setText(r.getName());
if(r.isRuleActive())
{
if (r.haveEnoughPermissions())
holder.ivActiveInactive.setImageResource(R.drawable.status_active);
else
holder.ivActiveInactive.setImageResource(R.drawable.status_unable);
}
else
holder.ivActiveInactive.setImageResource(R.drawable.status_inactive);
return v;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(AutomationService.isMyServiceRunning(this))
bindToService();
2021-06-20 22:24:17 +02:00
if(requestCode == requestCodeCreateRule) //add Rule
2021-02-16 13:42:49 +01:00
{
ruleToEdit = null; //clear cache
updateListView();
}
2021-06-20 22:24:17 +02:00
if(requestCode == requestCodeChangeRule) //editRule
2021-02-16 13:42:49 +01:00
{
ruleToEdit = null; //clear cache
updateListView();
}
AutomationService service = AutomationService.getInstance();
if(service != null)
service.applySettingsAndRules();
if(boundToService && AutomationService.isMyServiceRunning(this))
{
myAutomationService.serviceInterface(serviceCommands.updateNotification); //in case names got changed.
unBindFromService();
}
}
private AlertDialog getRuleDialog(final Rule ruleThisIsAbout)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(getResources().getString(R.string.whatToDoWithRule));
2021-06-12 02:27:51 +02:00
alertDialogBuilder.setItems(new String[]{ getResources().getString(R.string.runManually), getResources().getString(R.string.edit), getResources().getString(R.string.deleteCapital), getResources().getString(R.string.clone) }, new DialogInterface.OnClickListener()
2021-02-16 13:42:49 +01:00
{
@Override
public void onClick(DialogInterface dialog, int which)
{
switch(which)
{
case 0:
if(AutomationService.isMyServiceRunning(ActivityMainRules.this))
{
AutomationService runContext = AutomationService.getInstance();
if(runContext != null)
{
ruleThisIsAbout.activate(runContext, true);
break;
}
}
Toast.makeText(ActivityMainRules.this, getResources().getString(R.string.serviceHasToRunForThat), Toast.LENGTH_LONG).show();
break;
case 1:
ruleToEdit = ruleThisIsAbout;
2021-02-17 22:27:53 +01:00
Intent manageSpecificRuleIntent = new Intent (ActivityMainRules.this, ActivityManageRule.class);
2021-06-20 22:24:17 +02:00
startActivityForResult(manageSpecificRuleIntent, requestCodeChangeRule);
2021-02-16 13:42:49 +01:00
break;
case 2:
if(ruleThisIsAbout.delete())
2021-06-20 22:24:17 +02:00
{
ruleToEdit = null; //clear cache
2021-02-16 13:42:49 +01:00
updateListView();
2021-06-20 22:24:17 +02:00
}
2021-02-16 13:42:49 +01:00
break;
2021-06-12 02:27:51 +02:00
case 3:
ruleToEdit = ruleThisIsAbout;
if(ruleToEdit.cloneRule(ActivityMainRules.this))
2021-06-20 22:24:17 +02:00
{
ruleToEdit = null; //clear cache
2021-06-12 02:27:51 +02:00
updateListView();
2021-06-20 22:24:17 +02:00
}
2021-06-12 02:27:51 +02:00
break;
2021-02-16 13:42:49 +01:00
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
return alertDialog;
}
public void updateListView()
{
Miscellaneous.logEvent("i", "ListView", "Attempting to update RuleListView", 4);
2021-06-20 22:24:17 +02:00
ruleList.clear();
for(Rule r : Rule.getRuleCollection())
ruleList.add(r);
2021-02-16 13:42:49 +01:00
try
{
if(ruleListView.getAdapter() == null)
ruleListView.setAdapter(ruleListViewAdapter);
ruleListViewAdapter.notifyDataSetChanged();
}
catch(NullPointerException e)
{}
try
{
if(AutomationService.isMyServiceRunning(this))
2021-11-07 17:29:00 +01:00
DateTimeListener.reloadAlarms();
2021-02-16 13:42:49 +01:00
}
catch(NullPointerException e)
{
// AlarmManager instance not prepared, yet.
}
}
2021-06-20 22:24:17 +02:00
}