2015-03-25 22:23:45 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.os.Bundle;
|
2015-03-27 10:56:08 +01:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-03-26 18:11:47 +01:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
2015-03-25 22:23:45 +01:00
|
|
|
import android.widget.ListView;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAppAdapter;
|
|
|
|
|
|
|
|
|
|
|
|
public class AppManagerActivity extends Activity {
|
|
|
|
public static final String ACTION_REFRESH_APPLIST
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.appmanager.action.refresh_applist";
|
2015-03-27 10:56:08 +01:00
|
|
|
|
2015-03-25 22:23:45 +01:00
|
|
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
if (action.equals(ControlCenter.ACTION_QUIT)) {
|
|
|
|
finish();
|
|
|
|
} else if (action.equals(ACTION_REFRESH_APPLIST)) {
|
2015-03-26 18:11:47 +01:00
|
|
|
appList.clear();
|
2015-03-25 22:23:45 +01:00
|
|
|
int appCount = intent.getIntExtra("app_count", 0);
|
|
|
|
for (Integer i = 0; i < appCount; i++) {
|
|
|
|
String appName = intent.getStringExtra("app_name" + i.toString());
|
|
|
|
String appCreator = intent.getStringExtra("app_creator" + i.toString());
|
2015-03-26 18:11:47 +01:00
|
|
|
int id = intent.getIntExtra("app_id" + i.toString(), -1);
|
|
|
|
int index = intent.getIntExtra("app_index" + i.toString(), -1);
|
|
|
|
|
|
|
|
appList.add(new GBDeviceApp(id, index, appName, appCreator, ""));
|
2015-03-25 22:23:45 +01:00
|
|
|
}
|
|
|
|
mGBDeviceAppAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-03-26 18:11:47 +01:00
|
|
|
final List<GBDeviceApp> appList = new ArrayList<>();
|
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
|
|
|
private ListView appListView;
|
|
|
|
private GBDeviceAppAdapter mGBDeviceAppAdapter;
|
|
|
|
private GBDeviceApp selectedApp = null;
|
2015-03-25 22:23:45 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_appmanager);
|
|
|
|
|
|
|
|
appListView = (ListView) findViewById(R.id.appListView);
|
|
|
|
mGBDeviceAppAdapter = new GBDeviceAppAdapter(this, appList);
|
|
|
|
appListView.setAdapter(this.mGBDeviceAppAdapter);
|
2015-03-26 18:11:47 +01:00
|
|
|
registerForContextMenu(appListView);
|
2015-03-25 22:23:45 +01:00
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(ControlCenter.ACTION_QUIT);
|
|
|
|
filter.addAction(ACTION_REFRESH_APPLIST);
|
2015-03-27 10:56:08 +01:00
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
|
2015-03-25 22:23:45 +01:00
|
|
|
|
|
|
|
Intent startIntent = new Intent(this, BluetoothCommunicationService.class);
|
|
|
|
startIntent.setAction(BluetoothCommunicationService.ACTION_REQUEST_APPINFO);
|
|
|
|
startService(startIntent);
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:11:47 +01:00
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
super.onCreateContextMenu(menu, v, menuInfo);
|
|
|
|
getMenuInflater().inflate(
|
|
|
|
R.menu.appmanager_context, menu);
|
|
|
|
AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo;
|
|
|
|
selectedApp = appList.get(acmi.position);
|
|
|
|
menu.setHeaderTitle(selectedApp.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.appmanager_app_delete:
|
|
|
|
if (selectedApp != null) {
|
|
|
|
Intent deleteIntent = new Intent(this, BluetoothCommunicationService.class);
|
|
|
|
deleteIntent.setAction(BluetoothCommunicationService.ACTION_DELETEAPP);
|
|
|
|
deleteIntent.putExtra("app_id", selectedApp.getId());
|
|
|
|
deleteIntent.putExtra("app_index", selectedApp.getIndex());
|
|
|
|
startService(deleteIntent);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onContextItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 22:23:45 +01:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2015-03-27 10:56:08 +01:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
2015-03-25 22:23:45 +01:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
}
|