From 8e82f369c7fdc1a8c2a052ae53b5ac204cb5620e Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 25 Sep 2016 11:01:17 -0400 Subject: [PATCH] Implement Tasker/Locale plugin --- app/src/main/AndroidManifest.xml | 22 ++++ .../automation/EditSettingActivity.java | 50 ++++++++ .../automation/EditSettingController.java | 78 ++++++++++++ .../automation/EditSettingRootView.java | 105 ++++++++++++++++ .../automation/FireSettingReceiver.java | 114 ++++++++++++++++++ app/src/main/res/layout/automation.xml | 88 ++++++++++++++ app/src/main/res/values/constants.xml | 6 + app/src/main/res/values/strings.xml | 5 + 8 files changed, 468 insertions(+) create mode 100644 app/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java create mode 100644 app/src/main/java/org/isoron/uhabits/automation/EditSettingController.java create mode 100644 app/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java create mode 100644 app/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java create mode 100644 app/src/main/res/layout/automation.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 771c5fff2..1a958f1c1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -195,11 +195,33 @@ + + + + + + + + + + + + + + + diff --git a/app/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java b/app/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java new file mode 100644 index 000000000..d92e73480 --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.automation; + +import android.os.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.activities.*; +import org.isoron.uhabits.models.*; + +public class EditSettingActivity extends BaseActivity +{ + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + HabitsApplication app = (HabitsApplication) getApplicationContext(); + + HabitList habits = app.getComponent().getHabitList(); + habits = habits.getFiltered(new HabitMatcherBuilder() + .setArchivedAllowed(false) + .setCompletedAllowed(true) + .build()); + + EditSettingController controller = new EditSettingController(this); + EditSettingRootView rootView = + new EditSettingRootView(this, habits, controller); + + BaseScreen screen = new BaseScreen(this); + screen.setRootView(rootView); + setScreen(screen); + } +} diff --git a/app/src/main/java/org/isoron/uhabits/automation/EditSettingController.java b/app/src/main/java/org/isoron/uhabits/automation/EditSettingController.java new file mode 100644 index 000000000..478170496 --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/automation/EditSettingController.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.automation; + +import android.app.*; +import android.content.*; +import android.os.*; +import android.support.annotation.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.models.*; + +import static org.isoron.uhabits.automation.FireSettingReceiver.*; + +public class EditSettingController +{ + @NonNull + private final Activity activity; + + public EditSettingController(@NonNull Activity activity) + { + this.activity = activity; + } + + public void onSave(Habit habit, int action) + { + if (habit.getId() == null) return; + + String actionName = getActionName(action); + String blurb = String.format("%s: %s", actionName, habit.getName()); + + Bundle bundle = new Bundle(); + bundle.putInt("action", action); + bundle.putLong("habit", habit.getId()); + + Intent intent = new Intent(); + intent.putExtra(EXTRA_STRING_BLURB, blurb); + intent.putExtra(EXTRA_BUNDLE, bundle); + + activity.setResult(Activity.RESULT_OK, intent); + activity.finish(); + } + + private String getActionName(int action) + { + switch (action) + { + case ACTION_CHECK: + return activity.getString(R.string.check); + + case ACTION_UNCHECK: + return activity.getString(R.string.uncheck); + + case ACTION_TOGGLE: + return activity.getString(R.string.toggle); + + default: + return "???"; + } + } +} diff --git a/app/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java b/app/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java new file mode 100644 index 000000000..264ea8a22 --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.automation; + +import android.content.*; +import android.support.annotation.*; +import android.support.v7.widget.*; +import android.support.v7.widget.Toolbar; +import android.widget.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.activities.*; +import org.isoron.uhabits.models.*; +import org.isoron.uhabits.utils.*; + +import java.util.*; + +import butterknife.*; + +import static android.R.layout.*; + +public class EditSettingRootView extends BaseRootView +{ + @BindView(R.id.toolbar) + Toolbar toolbar; + + @BindView(R.id.habitSpinner) + AppCompatSpinner habitSpinner; + + @BindView(R.id.actionSpinner) + AppCompatSpinner actionSpinner; + + @NonNull + private final HabitList habitList; + + @NonNull + private final EditSettingController controller; + + public EditSettingRootView(@NonNull Context context, + @NonNull HabitList habitList, + @NonNull EditSettingController controller) + { + super(context); + this.habitList = habitList; + this.controller = controller; + + addView(inflate(getContext(), R.layout.automation, null)); + ButterKnife.bind(this); + populateHabitSpinner(); + } + + @NonNull + @Override + public Toolbar getToolbar() + { + return toolbar; + } + + @Override + public int getToolbarColor() + { + StyledResources res = new StyledResources(getContext()); + if (!res.getBoolean(R.attr.useHabitColorAsPrimary)) + return super.getToolbarColor(); + + return res.getColor(R.attr.aboutScreenColor); + } + + @OnClick(R.id.buttonSave) + public void onClickSave() + { + int action = actionSpinner.getSelectedItemPosition(); + int habitPosition = habitSpinner.getSelectedItemPosition(); + Habit habit = habitList.getByPosition(habitPosition); + controller.onSave(habit, action); + } + + private void populateHabitSpinner() + { + List names = new LinkedList<>(); + for (Habit h : habitList) names.add(h.getName()); + + ArrayAdapter adapter = + new ArrayAdapter<>(getContext(), simple_spinner_item, names); + adapter.setDropDownViewResource(simple_spinner_dropdown_item); + habitSpinner.setAdapter(adapter); + } +} diff --git a/app/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java b/app/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java new file mode 100644 index 000000000..7ff6af588 --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.automation; + +import android.content.*; +import android.os.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.models.*; +import org.isoron.uhabits.receivers.*; +import org.isoron.uhabits.utils.*; + +import dagger.*; + +public class FireSettingReceiver extends BroadcastReceiver +{ + public static final int ACTION_CHECK = 0; + + public static final int ACTION_UNCHECK = 1; + + public static final int ACTION_TOGGLE = 2; + + public static final String EXTRA_BUNDLE = + "com.twofortyfouram.locale.intent.extra.BUNDLE"; + + public static final String EXTRA_STRING_BLURB = + "com.twofortyfouram.locale.intent.extra.BLURB"; + + private HabitList allHabits; + + @Override + public void onReceive(Context context, Intent intent) + { + HabitsApplication app = + (HabitsApplication) context.getApplicationContext(); + + ReceiverComponent component = + DaggerFireSettingReceiver_ReceiverComponent + .builder() + .appComponent(app.getComponent()) + .build(); + + allHabits = app.getComponent().getHabitList(); + + Arguments args = parseIntent(intent); + if (args == null) return; + + long timestamp = DateUtils.getStartOfToday(); + WidgetController controller = component.getWidgetController(); + + switch (args.action) + { + case ACTION_CHECK: + controller.onAddRepetition(args.habit, timestamp); + break; + + case ACTION_UNCHECK: + controller.onRemoveRepetition(args.habit, timestamp); + break; + + case ACTION_TOGGLE: + controller.onToggleRepetition(args.habit, timestamp); + break; + } + } + + private Arguments parseIntent(Intent intent) + { + Arguments args = new Arguments(); + + Bundle bundle = intent.getBundleExtra(EXTRA_BUNDLE); + if (bundle == null) return null; + + args.action = bundle.getInt("action"); + if (args.action < 0 || args.action > 2) return null; + + Habit habit = allHabits.getById(bundle.getLong("habit")); + if (habit == null) return null; + args.habit = habit; + + return args; + } + + @ReceiverScope + @Component(dependencies = AppComponent.class) + interface ReceiverComponent + { + WidgetController getWidgetController(); + } + + private class Arguments + { + int action; + + Habit habit; + } +} diff --git a/app/src/main/res/layout/automation.xml b/app/src/main/res/layout/automation.xml new file mode 100644 index 000000000..f127a9f2d --- /dev/null +++ b/app/src/main/res/layout/automation.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +