Этот коммит содержится в:
Anton Tananaev
2017-08-31 13:48:52 +12:00
родитель e87c3c7783
Коммит 7c49bb78f3
6 изменённых файлов: 67 добавлений и 36 удалений
+1
Просмотреть файл
@@ -34,6 +34,7 @@
<activity android:name=".ShortcutActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
-18
Просмотреть файл
@@ -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) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
return true;
@@ -216,11 +203,6 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen
if (item.getItemId() == R.id.status) {
startActivity(new Intent(getActivity(), StatusActivity.class));
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) {
startActivity(new Intent(getActivity(), AboutActivity.class));
return true;
+64 -11
Просмотреть файл
@@ -22,29 +22,81 @@ import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.DrawableRes;
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.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ShortcutActivity extends AppCompatActivity {
public static final String EXTRA_ACTION = "action";
public static final String EXTRA_ACTION_START = "start";
public static final String EXTRA_ACTION_STOP = "stop";
public static final String EXTRA_ACTION_SOS = "sos";
public static final String ACTION_START = "start";
public static final String ACTION_STOP = "stop";
public static final String ACTION_SOS = "sos";
private static final String ALARM_SOS = "sos";
@Override
public void onCreate(Bundle 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
protected void onNewIntent(Intent 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")
@@ -83,34 +135,35 @@ public class ShortcutActivity extends AppCompatActivity {
}
}
private void checkShortcutAction(Intent intent) {
private boolean executeAction(Intent intent) {
String action;
if (intent.hasExtra("shortcutAction")) {
action = intent.getBooleanExtra("shortcutAction", false)
? EXTRA_ACTION_START : EXTRA_ACTION_STOP;
? ACTION_START : ACTION_STOP;
} else {
action = intent.getStringExtra(EXTRA_ACTION);
}
if (action != null) {
switch (action) {
case EXTRA_ACTION_START:
case ACTION_START:
PreferenceManager.getDefaultSharedPreferences(this)
.edit().putBoolean(MainFragment.KEY_STATUS, true).apply();
ContextCompat.startForegroundService(this, new Intent(this, TrackingService.class));
Toast.makeText(this, R.string.status_service_create, Toast.LENGTH_SHORT).show();
break;
case EXTRA_ACTION_STOP:
case ACTION_STOP:
PreferenceManager.getDefaultSharedPreferences(this)
.edit().putBoolean(MainFragment.KEY_STATUS, false).apply();
stopService(new Intent(this, TrackingService.class));
Toast.makeText(this, R.string.status_service_destroy, Toast.LENGTH_SHORT).show();
break;
case EXTRA_ACTION_SOS:
case ACTION_SOS:
sendAlarm();
break;
}
}
finish();
}
return action != null;
}
}
+1 -1
Просмотреть файл
@@ -62,7 +62,7 @@ public class StatusActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle 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);
ListView listView = findViewById(android.R.id.list);
listView.setAdapter(adapter);
Просмотреть файл
+1 -6
Просмотреть файл
@@ -6,12 +6,7 @@
<item
android:id="@+id/status"
android:title="@string/menu_status"
app:showAsAction="always" />
<item
android:id="@+id/shortcuts"
android:title="@string/menu_shortcuts"
app:showAsAction="never" />
app:showAsAction="ifRoom" />
<item
android:id="@+id/about"