1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 03:11:30 +00:00

Refresh drawer items when opening (fixes #217).

Also close the drawer when navigating away.
This commit is contained in:
Felix Ableitner 2015-02-27 22:57:28 +01:00
parent 489a15edb6
commit 9ed5fe0c20
6 changed files with 50 additions and 26 deletions

View file

@ -10,7 +10,7 @@ import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
@ -20,6 +20,7 @@ import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
@ -193,7 +194,7 @@ public class MainActivity extends SyncthingActivity
.beginTransaction()
.replace(R.id.drawer, mDrawerFragment)
.commit();
mDrawerToggle = new Toggle(this, mDrawerLayout, R.drawable.ic_drawer);
mDrawerToggle = new Toggle(this, mDrawerLayout);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@ -245,11 +246,7 @@ public class MainActivity extends SyncthingActivity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
@ -257,8 +254,8 @@ public class MainActivity extends SyncthingActivity
* Receives drawer opened and closed events.
*/
public class Toggle extends ActionBarDrawerToggle {
public Toggle(Activity activity, DrawerLayout drawerLayout, int drawerImageRes) {
super(activity, drawerLayout, drawerImageRes, R.string.app_name, R.string.app_name);
public Toggle(Activity activity, DrawerLayout drawerLayout) {
super(activity, drawerLayout, R.string.app_name, R.string.app_name);
}
@Override
@ -278,4 +275,11 @@ public class MainActivity extends SyncthingActivity
}
}
/**
* Closes the drawer. Use when navigating away from activity.
*/
public void closeDrawer() {
mDrawerLayout.closeDrawer(Gravity.START);
}
}

View file

@ -48,6 +48,8 @@ public class DrawerFragment extends Fragment implements RestApi.OnReceiveSystemI
private TextView mAnnounceServer;
private ListView mList;
private Timer mTimer;
private MainActivity mActivity;
@ -88,6 +90,12 @@ public class DrawerFragment extends Fragment implements RestApi.OnReceiveSystemI
}, 0, SyncthingService.GUI_UPDATE_INTERVAL);
}
@Override
public void onResume() {
super.onResume();
initMenu();
}
public void onDrawerClosed() {
mTimer.cancel();
mTimer = null;
@ -98,29 +106,37 @@ public class DrawerFragment extends Fragment implements RestApi.OnReceiveSystemI
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.drawer_fragment, container, false);
mDeviceId = (TextView) view.findViewById(R.id.device_id);
mCpuUsage = (TextView) view.findViewById(R.id.cpu_usage);
mRamUsage = (TextView) view.findViewById(R.id.ram_usage);
mDownload = (TextView) view.findViewById(R.id.download);
mUpload = (TextView) view.findViewById(R.id.upload);
View view = inflater.inflate(R.layout.drawer_fragment, container, false);
mDeviceId = (TextView) view.findViewById(R.id.device_id);
mCpuUsage = (TextView) view.findViewById(R.id.cpu_usage);
mRamUsage = (TextView) view.findViewById(R.id.ram_usage);
mDownload = (TextView) view.findViewById(R.id.download);
mUpload = (TextView) view.findViewById(R.id.upload);
mAnnounceServer = (TextView) view.findViewById(R.id.announce_server);
mList = (ListView) view.findViewById(android.R.id.list);
ArrayList<Pair<Integer, Integer>> items = new ArrayList<>();
items.add(new Pair<>(R.drawable.ic_action_share, R.string.share_device_id));
items.add(new Pair<>(R.drawable.ic_menu_browser, R.string.web_gui_title));
items.add(new Pair<>(R.drawable.ic_action_settings, R.string.settings_title));
items.add(new Pair<>(R.drawable.ic_action_donate, R.string.donate));
if (!SyncthingService.alwaysRunInBackground(getActivity())) {
items.add(new Pair<>(R.drawable.ic_menu_close_clear_cancel, R.string.exit));
}
ListView list = (ListView) view.findViewById(android.R.id.list);
list.setAdapter(new MenuAdapter(getActivity(), items));
list.setOnItemClickListener(this);
initMenu();
mList.setOnItemClickListener(this);
return view;
}
/**
* Repopulates menu items.
*/
private void initMenu() {
MenuAdapter adapter =
new MenuAdapter(getActivity(), new ArrayList<Pair<Integer, Integer>>());
adapter.add(new Pair<>(R.drawable.ic_action_share, R.string.share_device_id));
adapter.add(new Pair<>(R.drawable.ic_menu_browser, R.string.web_gui_title));
adapter.add(new Pair<>(R.drawable.ic_action_settings, R.string.settings_title));
adapter.add(new Pair<>(R.drawable.ic_action_donate, R.string.donate));
if (!SyncthingService.alwaysRunInBackground(getActivity()))
adapter.add(new Pair<>(R.drawable.ic_menu_close_clear_cancel, R.string.exit));
mList.setAdapter(adapter);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@ -197,18 +213,22 @@ public class DrawerFragment extends Fragment implements RestApi.OnReceiveSystemI
break;
case 1:
startActivity(new Intent(mActivity, WebGuiActivity.class));
mActivity.closeDrawer();
break;
case 2:
startActivity(new Intent(mActivity, SettingsActivity.class)
.setAction(SettingsActivity.ACTION_APP_SETTINGS_FRAGMENT));
mActivity.closeDrawer();
break;
case 3:
startActivity(new Intent(
Intent.ACTION_VIEW, Uri.parse(getString(R.string.donate_url))));
mActivity.closeDrawer();
break;
case 4:
mActivity.stopService(new Intent(mActivity, SyncthingService.class));
mActivity.finish();
mActivity.closeDrawer();
break;
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 B