Commit c5b521e1 authored by Bartek Fabiszewski's avatar Bartek Fabiszewski
Browse files

Feature: automation, handle external commands, closes #22

parent 76be3d79
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -33,10 +33,21 @@ led | tracking | synchronization

- clicking on current track's name will show track statistics

## Automating
- μlogger may accept commands from other applications for starting or stopping its operations. To make it work you must explicitly enable this functionality in app settings ("Allow external commands" switch). 
- Commands are sent as `broadcasts` with following `intent` parameters:
  - target package: `net.fabiszewski.ulogger`
  - target class: `net.fabiszewski.ulogger.ExternalCommandReceiver`
  - action: `net.fabiszewski.ulogger.intent.action.COMMAND`
  - extra: `"command": [command name]`, where command name is one of: `"start logger"`, `"stop logger"`, `"start upload"`, for starting and stopping position logging and starting track data upload to server (in case live tracking is off)
- Third party examples:
  - Automate (LlamaLab) – Send broadcast block with `Package`, `Receiver Class` and `Action` fields as above and `Extras` field eg. `{"command": "start logger"}
  - Tasker (joaomgcd) – System → Send intent. Fields `Action`, `Package`, `Class` as above and `Extra` field eg. `command:start logger`
- command line: `am broadcast -a net.fabiszewski.ulogger.intent.action.COMMAND -e "command" "start logger" net.fabiszewski.ulogger net.fabiszewski.ulogger.ExternalCommandReceiver`

## Contribute translations
[![Translate with transifex](https://img.shields.io/badge/translate-transifex-green.svg)](https://www.transifex.com/bfabiszewski/ulogger/)


## Donate
[![Donate paypal](https://img.shields.io/badge/donate-paypal-green.svg)](https://www.paypal.me/bfabiszewski)  
![Donate bitcoin](https://img.shields.io/badge/donate-bitcoin-green.svg) `bc1qt3uwhze9x8tj6v73c587gprhufg9uur0rzxhvh`  
+8 −0
Original line number Diff line number Diff line
@@ -71,6 +71,14 @@
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".ExternalCommandReceiver"
            android:exported="true"
            tools:ignore="ExportedReceiver">
            <intent-filter>
                <action android:name="net.fabiszewski.ulogger.intent.action.COMMAND" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
 No newline at end of file
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2019 Bartek Fabiszewski
 * http://www.fabiszewski.net
 *
 * This file is part of μlogger-android.
 * Licensed under GPL, either version 3, or any later.
 * See <http://www.gnu.org/licenses/>
 */

package net.fabiszewski.ulogger;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;

public class ExternalCommandReceiver extends BroadcastReceiver {

    private static final String START_LOGGER = "start logger";
    private static final String STOP_LOGGER = "stop logger";
    private static final String START_UPLOAD = "start upload";

    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean allowExternal = prefs.getBoolean(SettingsActivity.KEY_ALLOW_EXTERNAL, false);
        if (!allowExternal) {
            return;
        }
        if (intent != null) {
            String command = intent.getStringExtra("command");
            switch (command) {
                case START_LOGGER:
                    startLoggerService(context);
                    break;
                case STOP_LOGGER:
                    stopLogger(context);
                    break;
                case START_UPLOAD:
                    uploadData(context);
                    break;
            }
        }

    }


    /**
     * Start logger service
     * @param context Context
     */
    private void startLoggerService(Context context) {
        DbAccess db = DbAccess.getInstance();
        db.open(context);
        if (db.getTrackName() == null) {
            db.newAutoTrack();
        }
        db.close();
        Intent intent = new Intent(context, LoggerService.class);
        ContextCompat.startForegroundService(context, intent);
    }

    /**
     * Stop logger service
     * @param context Context
     */
    private void stopLogger(Context context) {
        Intent intent = new Intent(context, LoggerService.class);
        context.stopService(intent);
    }

    /**
     * Start logger service
     * @param context Context
     */
    private void uploadData(Context context) {
        DbAccess db = DbAccess.getInstance();
        db.open(context);
        if (db.needsSync()) {
            Intent intent = new Intent(context, WebSyncService.class);
            context.startService(intent);
        }
        db.close();
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ public class SettingsActivity extends AppCompatActivity {
    public static final String KEY_USE_GPS = "prefUseGps";
    public static final String KEY_USE_NET = "prefUseNet";
    public static final String KEY_LOGGER_RUNNING = "prefLoggerRunning";
    public static final String KEY_ALLOW_EXTERNAL = "prefAllowExternal";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
+3 −0
Original line number Diff line number Diff line
@@ -46,6 +46,9 @@
    <string name="pref_autostart_title">Auto start</string>
    <string name="pref_autostart_summary">Application will start on system boot</string>
    <string name="pref_autostart_default" translatable="false">false</string>
    <string name="pref_external_title">Allow external commands</string>
    <string name="pref_external_summary">Allow receiving commands from other applications to facilitate automation and tasks scheduling.</string>
    <string name="pref_external_default" translatable="false">false</string>
    <string name="pref_provider_title">Location provider</string>
    <string name="pref_provider_summary">Location may be supplied by gps provider, by network or by both. Both providers will give best results, but use more battery.</string>
    <string name="pref_units_title">Units</string>
Loading