add app shortcuts (Android 7.1+) for adding new transaction for top profiles
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 4 Apr 2019 11:35:06 +0000 (14:35 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 4 Apr 2019 11:35:06 +0000 (14:35 +0300)
app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java

index fa69f86..b670125 100644 (file)
@@ -24,9 +24,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectWriter;
 
 import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
-import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 import net.ktnx.mobileledger.utils.UrlEncodedFormData;
@@ -52,12 +52,14 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
     private String token;
     private String session;
     private LedgerTransaction ltr;
+    private MobileLedgerProfile mProfile;
 
-    public SendTransactionTask(TaskCallback callback) {
+    public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile) {
         taskCallback = callback;
+        mProfile = profile;
     }
     private boolean sendOK() throws IOException {
-        HttpURLConnection http = NetworkUtil.prepareConnection(Data.profile.get(), "add");
+        HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
         http.setRequestMethod("PUT");
         http.setRequestProperty("Content-Type", "application/json");
         http.setRequestProperty("Accept", "*/*");
@@ -104,7 +106,7 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         return true;
     }
     private boolean legacySendOK() throws IOException {
-        HttpURLConnection http = NetworkUtil.prepareConnection(Data.profile.get(), "add");
+        HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
         http.setRequestMethod("POST");
         http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
         http.setRequestProperty("Accept", "*/*");
index 3bfb2b6..a6c29db 100644 (file)
@@ -19,8 +19,12 @@ package net.ktnx.mobileledger.ui.activity;
 
 import android.content.Intent;
 import android.content.pm.PackageInfo;
+import android.content.pm.ShortcutInfo;
+import android.content.pm.ShortcutManager;
 import android.content.res.ColorStateList;
+import android.content.res.Resources;
 import android.graphics.Color;
+import android.graphics.drawable.Icon;
 import android.os.AsyncTask;
 import android.os.Build;
 import android.os.Bundle;
@@ -57,6 +61,7 @@ import net.ktnx.mobileledger.utils.MLDB;
 
 import java.lang.ref.WeakReference;
 import java.text.DateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Observer;
@@ -281,6 +286,7 @@ public class MainActivity extends ProfileThemedActivity {
         });
 
         setupProfile();
+        onProfileChanged(null);
 
         updateLastUpdateTextFromDB();
         Date lastUpdate = Data.lastUpdateDate.get();
@@ -295,6 +301,27 @@ public class MainActivity extends ProfileThemedActivity {
             scheduleTransactionListRetrieval();
         }
     }
+    private void createShortcuts() {
+        Resources rm = getResources();
+        List<ShortcutInfo> shortcuts = new ArrayList<>();
+        try (LockHolder lh = Data.profiles.lockForReading()) {
+            for (int i = 0; i < Data.profiles.size(); i++) {
+                MobileLedgerProfile p = Data.profiles.get(i);
+                if (!p.isPostingPermitted()) continue;
+
+                ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
+                        .setShortLabel(p.getName())
+                        .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
+                        .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
+                                NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid()))
+                        .setRank(i)
+                        .build();
+                shortcuts.add(si);
+            }
+        }
+        ShortcutManager sm = getSystemService(ShortcutManager.class);
+        sm.setDynamicShortcuts(shortcuts);
+    }
     private void onProfileListChanged(Object arg) {
         findViewById(R.id.nav_profile_list).setMinimumHeight(
                 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
@@ -303,6 +330,8 @@ public class MainActivity extends ProfileThemedActivity {
         Log.d("profiles", "profile list changed");
         if (arg == null) mProfileListAdapter.notifyDataSetChanged();
         else mProfileListAdapter.notifyItemChanged((int) arg);
+
+        createShortcuts();
     }
     private void onProfileChanged(Object arg) {
         MobileLedgerProfile profile = Data.profile.get();
@@ -669,7 +698,7 @@ public class MainActivity extends ProfileThemedActivity {
     }
     public void onAccountSummaryRowViewClicked(View view) {
         ViewGroup row;
-        if ( view.getId() == R.id.account_expander ) row = (ViewGroup) view.getParent().getParent();
+        if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent();
         else row = (ViewGroup) view.getParent();
 
         LedgerAccount acc = (LedgerAccount) row.getTag();
index 845afea..b471607 100644 (file)
@@ -93,7 +93,7 @@ public class NewTransactionActivity extends ProfileThemedActivity
         setContentView(R.layout.activity_new_transaction);
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
-        toolbar.setSubtitle(Data.profile.get().getName());
+        toolbar.setSubtitle(mProfile.getName());
 
         tvDate = findViewById(R.id.new_transaction_date);
         tvDate.setOnFocusChangeListener((v, hasFocus) -> {
@@ -122,7 +122,16 @@ public class NewTransactionActivity extends ProfileThemedActivity
 //            Log.d("swipe", "hooked to row "+i);
         }
     }
+    @Override
+    protected void initProfile() {
+        String profileUUID = getIntent().getStringExtra("profile_uuid");
 
+        if (profileUUID != null) {
+            mProfile = Data.getProfile(profileUUID);
+            if (mProfile == null) finish();
+        }
+        else super.initProfile();
+    }
     @Override
     public void finish() {
         super.finish();
@@ -149,7 +158,7 @@ public class NewTransactionActivity extends ProfileThemedActivity
         progress.setVisibility(View.VISIBLE);
         try {
 
-            saver = new SendTransactionTask(this);
+            saver = new SendTransactionTask(this, mProfile);
 
             String dateString = tvDate.getText().toString();
             Date date;
@@ -180,7 +189,8 @@ public class NewTransactionActivity extends ProfileThemedActivity
                 tr.addAccount(item);
             }
 
-            if (emptyAmountAccount != null) emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
+            if (emptyAmountAccount != null)
+                emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
             saver.execute(tr);
         }
         catch (ParseException e) {
index 36e937d..fb6db90 100644 (file)
@@ -20,14 +20,17 @@ package net.ktnx.mobileledger.ui.activity;
 import android.annotation.SuppressLint;
 import android.os.Bundle;
 
+import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.utils.Colors;
 
 import androidx.annotation.Nullable;
 
 @SuppressLint("Registered")
 public class ProfileThemedActivity extends CrashReportingActivity {
+    protected MobileLedgerProfile mProfile;
     protected void setupProfileColors() {
-        Colors.setupTheme(this);
+        Colors.setupTheme(this, mProfile);
     }
     @Override
     protected void onStart() {
@@ -35,7 +38,11 @@ public class ProfileThemedActivity extends CrashReportingActivity {
         Colors.refreshColors(getTheme());
     }
     protected void onCreate(@Nullable Bundle savedInstanceState) {
+        initProfile();
         super.onCreate(savedInstanceState);
-        Colors.setupTheme(this);
+        setupProfileColors();
+    }
+    protected void initProfile() {
+        mProfile = Data.profile.get();
     }
 }