playing around with fragments

This commit is contained in:
Andreas Shimokawa 2016-06-14 15:56:38 +02:00
parent 98999993e5
commit 1d6a697000
6 changed files with 156 additions and 39 deletions

View File

@ -55,7 +55,7 @@
android:parentActivityName=".activities.SettingsActivity" /> android:parentActivityName=".activities.SettingsActivity" />
<activity <activity
android:launchMode="singleTop" android:launchMode="singleTop"
android:name=".activities.AppManagerActivity" android:name=".activities.appmanager.AppManagerActivity"
android:label="@string/title_activity_appmanager" android:label="@string/title_activity_appmanager"
android:parentActivityName=".activities.ControlCenter" /> android:parentActivityName=".activities.ControlCenter" />
<activity <activity
@ -263,7 +263,7 @@
<activity <activity
android:name=".activities.ExternalPebbleJSActivity" android:name=".activities.ExternalPebbleJSActivity"
android:label="@string/app_configure" android:label="@string/app_configure"
android:parentActivityName=".activities.AppManagerActivity"> android:parentActivityName=".activities.appmanager.AppManagerActivity">
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value="nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter" /> android:value="nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter" />

View File

@ -1,4 +1,4 @@
package nodomain.freeyourgadget.gadgetbridge.activities; package nodomain.freeyourgadget.gadgetbridge.activities.appmanager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@ -6,11 +6,14 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.NavUtils; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
@ -27,6 +30,7 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.ExternalPebbleJSActivity;
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAppAdapter; import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAppAdapter;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
@ -36,17 +40,17 @@ import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class AppManagerActivity extends GBActivity { public class AbstractAppManagerFragment extends Fragment {
public static final String ACTION_REFRESH_APPLIST public static final String ACTION_REFRESH_APPLIST
= "nodomain.freeyourgadget.gadgetbridge.appmanager.action.refresh_applist"; = "nodomain.freeyourgadget.gadgetbridge.appmanager.action.refresh_applist";
private static final Logger LOG = LoggerFactory.getLogger(AppManagerActivity.class); private static final Logger LOG = LoggerFactory.getLogger(AbstractAppManagerFragment.class);
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if (action.equals(GBApplication.ACTION_QUIT)) { if (action.equals(GBApplication.ACTION_QUIT)) {
finish(); // finish();
} else if (action.equals(ACTION_REFRESH_APPLIST)) { } else if (action.equals(ACTION_REFRESH_APPLIST)) {
int appCount = intent.getIntExtra("app_count", 0); int appCount = intent.getIntExtra("app_count", 0);
for (Integer i = 0; i < appCount; i++) { for (Integer i = 0; i < appCount; i++) {
@ -131,22 +135,30 @@ public class AppManagerActivity extends GBActivity {
} }
@Override @Override
protected void onCreate(Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); prefs = GBApplication.getPrefs();
appList.addAll(getCachedApps());
Bundle extras = getIntent().getExtras(); appList.addAll(getSystemApps());
if (extras != null) {
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE); IntentFilter filter = new IntentFilter();
} else { filter.addAction(GBApplication.ACTION_QUIT);
throw new IllegalArgumentException("Must provide a device when invoking this activity"); filter.addAction(ACTION_REFRESH_APPLIST);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(mReceiver, filter);
GBApplication.deviceService().onAppInfoReq();
super.onCreate(savedInstanceState);
} }
prefs = GBApplication.getPrefs(); @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setContentView(R.layout.activity_appmanager); View rootView = inflater.inflate(R.layout.activity_appmanager, container, false);
ListView appListView = (ListView) findViewById(R.id.appListView); ListView appListView = (ListView) (rootView.findViewById(R.id.appListView));
mGBDeviceAppAdapter = new GBDeviceAppAdapter(this, appList); mGBDeviceAppAdapter = new GBDeviceAppAdapter(getContext(), appList);
appListView.setAdapter(this.mGBDeviceAppAdapter); appListView.setAdapter(this.mGBDeviceAppAdapter);
appListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { appListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@ -159,23 +171,13 @@ public class AppManagerActivity extends GBActivity {
registerForContextMenu(appListView); registerForContextMenu(appListView);
appList.addAll(getCachedApps()); return rootView;
appList.addAll(getSystemApps());
IntentFilter filter = new IntentFilter();
filter.addAction(GBApplication.ACTION_QUIT);
filter.addAction(ACTION_REFRESH_APPLIST);
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
GBApplication.deviceService().onAppInfoReq();
} }
@Override @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.appmanager_context, menu); getActivity().getMenuInflater().inflate(R.menu.appmanager_context, menu);
AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo; AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo;
selectedApp = appList.get(acmi.position); selectedApp = appList.get(acmi.position);
@ -254,7 +256,7 @@ public class AppManagerActivity extends GBActivity {
case R.id.appmanager_app_configure: case R.id.appmanager_app_configure:
GBApplication.deviceService().onAppStart(selectedApp.getUUID(), true); GBApplication.deviceService().onAppStart(selectedApp.getUUID(), true);
Intent startIntent = new Intent(getApplicationContext(), ExternalPebbleJSActivity.class); Intent startIntent = new Intent(getContext().getApplicationContext(), ExternalPebbleJSActivity.class);
startIntent.putExtra("app_uuid", selectedApp.getUUID()); startIntent.putExtra("app_uuid", selectedApp.getUUID());
startIntent.putExtra(GBDevice.EXTRA_DEVICE, mGBDevice); startIntent.putExtra(GBDevice.EXTRA_DEVICE, mGBDevice);
startActivity(startIntent); startActivity(startIntent);
@ -271,15 +273,15 @@ public class AppManagerActivity extends GBActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
NavUtils.navigateUpFromSameTask(this); // NavUtils.navigateUpFromSameTask(this);
return true; return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override @Override
protected void onDestroy() { public void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mReceiver);
super.onDestroy(); super.onDestroy();
} }
} }

View File

@ -0,0 +1,83 @@
package nodomain.freeyourgadget.gadgetbridge.activities.appmanager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractFragmentPagerAdapter;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBFragmentActivity;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class AppManagerActivity extends AbstractGBFragmentActivity {
private GBDevice mGBDevice = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragmentappmanager);
Bundle extras = getIntent().getExtras();
if (extras != null) {
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
} else {
throw new IllegalArgumentException("Must provide a device when invoking this activity");
}
// Set up the ViewPager with the sections adapter.
ViewPager viewPager = (ViewPager) findViewById(R.id.appmanager_pager);
viewPager.setAdapter(getPagerAdapter());
}
@Override
protected AbstractFragmentPagerAdapter createFragmentPagerAdapter(FragmentManager fragmentManager) {
return new SectionsPagerAdapter(fragmentManager);
}
public class SectionsPagerAdapter extends AbstractFragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
switch (position) {
case 0:
case 1:
case 2:
AbstractAppManagerFragment fragment = new AbstractAppManagerFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("GBDevice", mGBDevice);
return fragment;
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "test";
case 1:
return "for";
case 2:
return "me";
case 3:
}
return super.getPageTitle(position);
}
}
}

View File

@ -6,7 +6,7 @@ import android.net.Uri;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AppManagerActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;

View File

@ -24,7 +24,7 @@ import java.util.Objects;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AbstractAppManagerFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
@ -160,7 +160,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
Context context = getContext(); Context context = getContext();
LOG.info("Got event for APP_INFO"); LOG.info("Got event for APP_INFO");
Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST); Intent appInfoIntent = new Intent(AbstractAppManagerFragment.ACTION_REFRESH_APPLIST);
int appCount = appInfoEvent.apps.length; int appCount = appInfoEvent.apps.length;
appInfoIntent.putExtra("app_count", appCount); appInfoIntent.putExtra("app_count", appCount);
for (Integer i = 0; i < appCount; i++) { for (Integer i = 0; i < appCount; i++) {

View File

@ -0,0 +1,32 @@
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px"
tools:context=".activities.appmanager.AppManagerActivity">
<LinearLayout
android:id="@+id/charts_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/appmanager_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.appmanager.AppManagerActivity">
<android.support.v4.view.PagerTabStrip
android:id="@+id/charts_pagerTabStrip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</android.support.v4.view.ViewPager>
</LinearLayout>
</android.widget.RelativeLayout>