Implement application shortcuts (fix #161)

Этот коммит содержится в:
Anton Tananaev
2016-01-30 10:17:30 +13:00
родитель bc3f47e296
Коммит b7fb6445ef
5 изменённых файлов: 94 добавлений и 11 удалений
+8
Просмотреть файл
@@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@@ -30,6 +31,13 @@
<activity android:name=".AboutActivity"/> <activity android:name=".AboutActivity"/>
<activity
android:name=".ShortcutActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name=".TrackingService" /> <service android:name=".TrackingService" />
<service android:name=".TrackingService$HideNotificationService" /> <service android:name=".TrackingService$HideNotificationService" />
+21 -10
Просмотреть файл
@@ -1,5 +1,5 @@
/* /*
* Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com) * Copyright 2012 - 2016 Anton Tananaev (anton.tananaev@gmail.com)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,16 +17,11 @@ package org.traccar.client;
import android.Manifest; import android.Manifest;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
@@ -34,9 +29,7 @@ import android.preference.EditTextPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.TwoStatePreference; import android.preference.TwoStatePreference;
import android.telephony.TelephonyManager;
import android.util.Patterns; import android.util.Patterns;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@@ -74,7 +67,7 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
findPreference(KEY_DEVICE).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { findPreference(KEY_DEVICE).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
return newValue != null && !((String) newValue).isEmpty(); return newValue != null && !newValue.equals("");
} }
}); });
findPreference(KEY_ADDRESS).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { findPreference(KEY_ADDRESS).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@@ -137,6 +130,20 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
} }
} }
private void addShortcuts(boolean start, int name) {
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setComponent(new ComponentName(getPackageName(), ".ShortcutActivity"));
shortcutIntent.putExtra(ShortcutActivity.EXTRA_ACTION, start);
Intent installShortCutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(name));
installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher));
sendBroadcast(installShortCutIntent);
}
private boolean hasPermission(String permission) { private boolean hasPermission(String permission) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
return true; return true;
@@ -188,6 +195,10 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
if (item.getItemId() == R.id.status) { if (item.getItemId() == R.id.status) {
startActivity(new Intent(this, StatusActivity.class)); startActivity(new Intent(this, StatusActivity.class));
return true; return true;
} else if (item.getItemId() == R.id.shortcuts) {
addShortcuts(true, R.string.shortcut_start);
addShortcuts(false, R.string.shortcut_stop);
return true;
} else if (item.getItemId() == R.id.about) { } else if (item.getItemId() == R.id.about) {
startActivity(new Intent(this, AboutActivity.class)); startActivity(new Intent(this, AboutActivity.class));
return true; return true;
@@ -208,7 +219,7 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
private void startTrackingService(boolean checkPermission, boolean permission) { private void startTrackingService(boolean checkPermission, boolean permission) {
if (checkPermission) { if (checkPermission) {
Set<String> missingPermissions = new HashSet<String>(); Set<String> missingPermissions = new HashSet<>();
if (!hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)) { if (!hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
missingPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION); missingPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
} }
+56
Просмотреть файл
@@ -0,0 +1,56 @@
/*
* Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.client;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;
public class ShortcutActivity extends Activity {
public static final String EXTRA_ACTION = "shortcutAction";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkShortcutAction(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
checkShortcutAction(intent);
}
private void checkShortcutAction(Intent intent) {
if (intent.hasExtra(EXTRA_ACTION)) {
boolean start = intent.getBooleanExtra(EXTRA_ACTION, false);
PreferenceManager.getDefaultSharedPreferences(this)
.edit().putBoolean(MainActivity.KEY_STATUS, start).commit();
if (start) {
startService(new Intent(this, TrackingService.class));
Toast.makeText(this, R.string.status_service_create, Toast.LENGTH_SHORT).show();
} else {
stopService(new Intent(this, TrackingService.class));
Toast.makeText(this, R.string.status_service_destroy, Toast.LENGTH_SHORT).show();
}
finish();
}
}
}
+6 -1
Просмотреть файл
@@ -6,9 +6,14 @@
android:title="@string/menu_status" android:title="@string/menu_status"
android:showAsAction="always" /> android:showAsAction="always" />
<item
android:id="@+id/shortcuts"
android:title="@string/menu_shortcuts"
android:showAsAction="never" />
<item <item
android:id="@+id/about" android:id="@+id/about"
android:title="@string/menu_about" android:title="@string/menu_about"
android:showAsAction="ifRoom" /> android:showAsAction="never" />
</menu> </menu>
+3
Просмотреть файл
@@ -3,6 +3,8 @@
<string name="app_name">Traccar Client</string> <string name="app_name">Traccar Client</string>
<string name="app_logo">Traccar</string> <string name="app_logo">Traccar</string>
<string name="shortcut_start">Traccar - Start</string>
<string name="shortcut_stop">Traccar - Stop</string>
<string name="settings_id_title">Device identifier</string> <string name="settings_id_title">Device identifier</string>
<string name="settings_address_title">Server address</string> <string name="settings_address_title">Server address</string>
@@ -28,6 +30,7 @@
<string name="menu_status">Status</string> <string name="menu_status">Status</string>
<string name="menu_about">About</string> <string name="menu_about">About</string>
<string name="menu_shortcuts">Add shortcuts</string>
<string name="menu_clear">Clear</string> <string name="menu_clear">Clear</string>
<string name="about_description">Real time GPS tracker for Android devices. Compatible with Traccar Server and other tracking systems.</string> <string name="about_description">Real time GPS tracker for Android devices. Compatible with Traccar Server and other tracking systems.</string>