Implement proper shortcuts
Этот коммит содержится в:
@@ -34,6 +34,7 @@
|
|||||||
<activity android:name=".ShortcutActivity">
|
<activity android:name=".ShortcutActivity">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<action android:name="android.intent.action.CREATE_SHORTCUT" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
|
|||||||
@@ -151,19 +151,6 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addShortcuts(String action, int name) {
|
|
||||||
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
|
|
||||||
shortcutIntent.setComponent(new ComponentName(getActivity().getPackageName(), ShortcutActivity.class.getCanonicalName()));
|
|
||||||
shortcutIntent.putExtra(ShortcutActivity.EXTRA_ACTION, action);
|
|
||||||
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(getActivity(), R.mipmap.ic_launcher));
|
|
||||||
|
|
||||||
getActivity().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;
|
||||||
@@ -216,11 +203,6 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen
|
|||||||
if (item.getItemId() == R.id.status) {
|
if (item.getItemId() == R.id.status) {
|
||||||
startActivity(new Intent(getActivity(), StatusActivity.class));
|
startActivity(new Intent(getActivity(), StatusActivity.class));
|
||||||
return true;
|
return true;
|
||||||
} else if (item.getItemId() == R.id.shortcuts) {
|
|
||||||
addShortcuts(ShortcutActivity.EXTRA_ACTION_START, R.string.shortcut_start);
|
|
||||||
addShortcuts(ShortcutActivity.EXTRA_ACTION_STOP, R.string.shortcut_stop);
|
|
||||||
addShortcuts(ShortcutActivity.EXTRA_ACTION_SOS, R.string.shortcut_sos);
|
|
||||||
return true;
|
|
||||||
} else if (item.getItemId() == R.id.about) {
|
} else if (item.getItemId() == R.id.about) {
|
||||||
startActivity(new Intent(getActivity(), AboutActivity.class));
|
startActivity(new Intent(getActivity(), AboutActivity.class));
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -22,29 +22,81 @@ import android.location.Location;
|
|||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.annotation.DrawableRes;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.support.v4.content.pm.ShortcutInfoCompat;
|
||||||
|
import android.support.v4.content.pm.ShortcutManagerCompat;
|
||||||
|
import android.support.v4.graphics.drawable.IconCompat;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.ListView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
public class ShortcutActivity extends AppCompatActivity {
|
public class ShortcutActivity extends AppCompatActivity {
|
||||||
|
|
||||||
public static final String EXTRA_ACTION = "action";
|
public static final String EXTRA_ACTION = "action";
|
||||||
public static final String EXTRA_ACTION_START = "start";
|
public static final String ACTION_START = "start";
|
||||||
public static final String EXTRA_ACTION_STOP = "stop";
|
public static final String ACTION_STOP = "stop";
|
||||||
public static final String EXTRA_ACTION_SOS = "sos";
|
public static final String ACTION_SOS = "sos";
|
||||||
|
|
||||||
private static final String ALARM_SOS = "sos";
|
private static final String ALARM_SOS = "sos";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
checkShortcutAction(getIntent());
|
if (!executeAction(getIntent())) {
|
||||||
|
setContentView(R.layout.list);
|
||||||
|
|
||||||
|
final String[] items = new String[] {
|
||||||
|
getString(R.string.shortcut_start),
|
||||||
|
getString(R.string.shortcut_stop),
|
||||||
|
getString(R.string.shortcut_sos)
|
||||||
|
};
|
||||||
|
|
||||||
|
ListView listView = findViewById(android.R.id.list);
|
||||||
|
|
||||||
|
listView.setAdapter(new ArrayAdapter<>(
|
||||||
|
this, android.R.layout.simple_list_item_1, items));
|
||||||
|
|
||||||
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
|
switch (position) {
|
||||||
|
case 0:
|
||||||
|
setShortcutResult(items[position], R.mipmap.ic_launcher, ACTION_START);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
setShortcutResult(items[position], R.mipmap.ic_launcher, ACTION_STOP);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
setShortcutResult(items[position], R.mipmap.ic_launcher, ACTION_SOS);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onNewIntent(Intent intent) {
|
protected void onNewIntent(Intent intent) {
|
||||||
super.onNewIntent(intent);
|
super.onNewIntent(intent);
|
||||||
checkShortcutAction(intent);
|
executeAction(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setShortcutResult(String label, @DrawableRes int iconResId, String action) {
|
||||||
|
Intent intent = new Intent(Intent.ACTION_MAIN, null, this, ShortcutActivity.class);
|
||||||
|
intent.putExtra(EXTRA_ACTION, action);
|
||||||
|
|
||||||
|
ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(this, action)
|
||||||
|
.setShortLabel(label)
|
||||||
|
.setIcon(IconCompat.createWithResource(this, iconResId))
|
||||||
|
.setIntent(intent)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, shortcut));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("MissingPermission")
|
@SuppressWarnings("MissingPermission")
|
||||||
@@ -83,34 +135,35 @@ public class ShortcutActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkShortcutAction(Intent intent) {
|
private boolean executeAction(Intent intent) {
|
||||||
String action;
|
String action;
|
||||||
if (intent.hasExtra("shortcutAction")) {
|
if (intent.hasExtra("shortcutAction")) {
|
||||||
action = intent.getBooleanExtra("shortcutAction", false)
|
action = intent.getBooleanExtra("shortcutAction", false)
|
||||||
? EXTRA_ACTION_START : EXTRA_ACTION_STOP;
|
? ACTION_START : ACTION_STOP;
|
||||||
} else {
|
} else {
|
||||||
action = intent.getStringExtra(EXTRA_ACTION);
|
action = intent.getStringExtra(EXTRA_ACTION);
|
||||||
}
|
}
|
||||||
if (action != null) {
|
if (action != null) {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case EXTRA_ACTION_START:
|
case ACTION_START:
|
||||||
PreferenceManager.getDefaultSharedPreferences(this)
|
PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
.edit().putBoolean(MainFragment.KEY_STATUS, true).apply();
|
.edit().putBoolean(MainFragment.KEY_STATUS, true).apply();
|
||||||
ContextCompat.startForegroundService(this, new Intent(this, TrackingService.class));
|
ContextCompat.startForegroundService(this, new Intent(this, TrackingService.class));
|
||||||
Toast.makeText(this, R.string.status_service_create, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.status_service_create, Toast.LENGTH_SHORT).show();
|
||||||
break;
|
break;
|
||||||
case EXTRA_ACTION_STOP:
|
case ACTION_STOP:
|
||||||
PreferenceManager.getDefaultSharedPreferences(this)
|
PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
.edit().putBoolean(MainFragment.KEY_STATUS, false).apply();
|
.edit().putBoolean(MainFragment.KEY_STATUS, false).apply();
|
||||||
stopService(new Intent(this, TrackingService.class));
|
stopService(new Intent(this, TrackingService.class));
|
||||||
Toast.makeText(this, R.string.status_service_destroy, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.status_service_destroy, Toast.LENGTH_SHORT).show();
|
||||||
break;
|
break;
|
||||||
case EXTRA_ACTION_SOS:
|
case ACTION_SOS:
|
||||||
sendAlarm();
|
sendAlarm();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
finish();
|
return action != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class StatusActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.status);
|
setContentView(R.layout.list);
|
||||||
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, messages);
|
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, messages);
|
||||||
ListView listView = findViewById(android.R.id.list);
|
ListView listView = findViewById(android.R.id.list);
|
||||||
listView.setAdapter(adapter);
|
listView.setAdapter(adapter);
|
||||||
|
|||||||
@@ -6,12 +6,7 @@
|
|||||||
<item
|
<item
|
||||||
android:id="@+id/status"
|
android:id="@+id/status"
|
||||||
android:title="@string/menu_status"
|
android:title="@string/menu_status"
|
||||||
app:showAsAction="always" />
|
app:showAsAction="ifRoom" />
|
||||||
|
|
||||||
<item
|
|
||||||
android:id="@+id/shortcuts"
|
|
||||||
android:title="@string/menu_shortcuts"
|
|
||||||
app:showAsAction="never" />
|
|
||||||
|
|
||||||
<item
|
<item
|
||||||
android:id="@+id/about"
|
android:id="@+id/about"
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user