Commit 396fed97 authored by David Papazian's avatar David Papazian Committed by Geoffrey Métais

Improve extensions restauration and destruction

Signed-off-by: default avatarGeoffrey Métais <geoffrey.metais@gmail.com>
parent 8b758ea4
......@@ -73,29 +73,21 @@ public class ExtensionsManager {
}
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
if (context instanceof MainActivity) {
MainActivity activity = (MainActivity) context;
if (deleteUnusedExtensionPreferences(extensions, settings)) {
if (activity.currentIdIsExtension()) {
//case: an extension is missing and current was an extension
activity.setCurrentFragmentId(-1);
settings.edit().putInt("fragment_id", -1).apply();
}
} else {
if (activity.currentIdIsExtension()) {
int currentExtensionId = activity.getCurrentFragmentId();
if (extensionIsEnabled(settings, currentExtensionId)) {
String currentExtensionTitle = mExtensions.get(currentExtensionId).title();
for (int i = 0; i < extensions.size(); ++i) {
if (TextUtils.equals(extensions.get(i).title(), currentExtensionTitle)) {
activity.setCurrentFragmentId(i);
settings.edit().putInt("fragment_id", i).apply();
break;
}
}
deleteUnusedExtensionPreferences(extensions, settings);
if (context instanceof MainActivity && ((MainActivity)context).currentIdIsExtension()) {
if (previousExtensionIsEnabled(context)) {
String lastExtensionTitle = settings.getString("current_extension_name", null);
for (int i = 0; i < extensions.size(); ++i) {
if (TextUtils.equals(extensions.get(i).title(), lastExtensionTitle)) {
((MainActivity)context).setCurrentFragmentId(i);
settings.edit().putInt("fragment_id", i).apply();
break;
}
}
} else {
((MainActivity)context).setCurrentFragmentId(-1);
settings.edit().putInt("fragment_id", -1).apply();
}
}
......@@ -113,13 +105,10 @@ public class ExtensionsManager {
return mExtensions;
}
public boolean extensionIsEnabled(SharedPreferences settings, int id) {
if (id == -1) {
return false;
} else {
String key = "extension_" + mExtensions.get(id).componentName().getPackageName();
return settings.getBoolean(key, false);
}
public boolean previousExtensionIsEnabled(Context context) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
String key = "extension_" + settings.getString("current_extension_name", null);
return settings.contains(key) && settings.getBoolean(key, false);
}
private boolean deleteUnusedExtensionPreferences(List<ExtensionListing> list, SharedPreferences settings) {
......@@ -140,7 +129,7 @@ public class ExtensionsManager {
MenuItem extensionGroup = ((NavigationView)activity.findViewById(R.id.navigation)).getMenu().findItem(R.id.extensions_group);
extensionGroup.setVisible(true);
MenuItem item = extensionGroup.getSubMenu().add(R.id.extensions_group, id, 0, extension.title());
item.setCheckable(true);
item.setCheckable(false);
int iconRes = extension.menuIcon();
Drawable extensionIcon = null;
if (iconRes != 0) {
......
......@@ -251,17 +251,11 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
@Override
protected void onStart() {
super.onStart();
if (BuildConfig.DEBUG)
createExtensionServiceConnection();
}
@Override
protected void onStop() {
super.onStop();
if (mExtensionServiceConnection != null) {
unbindService(mExtensionServiceConnection);
mExtensionServiceConnection = null;
}
}
private void loadPlugins() {
......@@ -290,8 +284,8 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
}
private void onPluginsLoaded() {
if (currentIdIsExtension())
if (mExtensionsManager.extensionIsEnabled(mSettings, mCurrentFragmentId))
if (mCurrentFragment == null && currentIdIsExtension())
if (mExtensionsManager.previousExtensionIsEnabled(getApplication()))
mExtensionManagerService.openExtension(mCurrentFragmentId);
else
showFragment(R.id.nav_video);
......@@ -319,6 +313,8 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
@Override
protected void onResume() {
super.onResume();
if (BuildConfig.DEBUG)
createExtensionServiceConnection();
mCurrentFragmentId = mSettings.getInt("fragment_id", R.id.nav_video);
if (mMediaLibrary.isInitiated()) {
/* Load media items from database and storage */
......@@ -352,16 +348,20 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
}
/* Save the tab status in pref */
mSettings.edit().putInt("fragment_id", mCurrentFragmentId).apply();
if (mExtensionServiceConnection != null) {
unbindService(mExtensionServiceConnection);
mExtensionServiceConnection = null;
}
if (currentIdIsExtension())
mSettings.edit()
.putString("current_extension_name", mExtensionsManager.getExtensions(getApplication(), false).get(mCurrentFragmentId).componentName().getPackageName())
.apply();
}
protected void onSaveInstanceState(Bundle outState) {
if (mCurrentFragment instanceof ExtensionBrowser) {
while (getSupportFragmentManager().popBackStackImmediate()) {}
if (mCurrentFragment != null) {
getSupportFragmentManager().beginTransaction().remove(mCurrentFragment).commit();
mCurrentFragment = null;
}
}
if (mCurrentFragment instanceof ExtensionBrowser)
mCurrentFragment = null;
super.onSaveInstanceState(outState);
outState.putInt("current", mCurrentFragmentId);
}
......@@ -449,6 +449,7 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
mCurrentFragment = fragment;
}
ft.commit();
mNavigationView.getMenu().findItem(extensionId).setCheckable(true);
updateCheckedItem(extensionId);
mCurrentFragmentId = extensionId;
}
......
......@@ -46,15 +46,16 @@ public class ExtensionBrowser extends Fragment implements View.OnClickListener,
private static final int REFRESH_TIMEOUT = 5000;
private String mTitle;
FloatingActionButton mAddDirectoryFAB;
ExtensionAdapter mAdapter;
private FloatingActionButton mAddDirectoryFAB;
private ExtensionAdapter mAdapter;
protected ContextMenuRecyclerView mRecyclerView;
protected LinearLayoutManager mLayoutManager;
protected TextView mEmptyView;
protected SwipeRefreshLayout mSwipeRefreshLayout;
ExtensionManagerService mExtensionManagerService;
private ExtensionManagerService mExtensionManagerService;
private boolean showSettings = false;
private boolean mustBeTerminated = false;
public void setExtensionService(ExtensionManagerService service) {
mExtensionManagerService = service;
......@@ -69,10 +70,10 @@ public class ExtensionBrowser extends Fragment implements View.OnClickListener,
super.onCreate(bundle);
if (bundle == null)
bundle = getArguments();
if (bundle != null){
if (bundle != null) {
mTitle = bundle.getString(KEY_TITLE);
showSettings = bundle.getBoolean(KEY_SHOW_FAB);
List<VLCExtensionItem> list = bundle.<VLCExtensionItem>getParcelableArrayList(KEY_ITEMS_LIST);
List<VLCExtensionItem> list = bundle.getParcelableArrayList(KEY_ITEMS_LIST);
if (list != null)
mAdapter.addAll(list);
}
......@@ -81,8 +82,8 @@ public class ExtensionBrowser extends Fragment implements View.OnClickListener,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.directory_browser, container, false);
mRecyclerView = (ContextMenuRecyclerView) v.findViewById(R.id.network_list);
mEmptyView = (TextView) v.findViewById(android.R.id.empty);
mRecyclerView = v.findViewById(R.id.network_list);
mEmptyView = v.findViewById(android.R.id.empty);
mEmptyView.setText(R.string.extension_empty);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.addItemDecoration(new DividerItemDecoration(VLCApplication.getAppContext(), DividerItemDecoration.VERTICAL));
......@@ -90,19 +91,27 @@ public class ExtensionBrowser extends Fragment implements View.OnClickListener,
mRecyclerView.setAdapter(mAdapter);
registerForContextMenu(mRecyclerView);
mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeLayout);
mSwipeRefreshLayout = v.findViewById(R.id.swipeLayout);
mSwipeRefreshLayout.setOnRefreshListener(this);
return v;
}
@Override
public void onResume() {
super.onResume();
if (mustBeTerminated)
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
mustBeTerminated = true;
}
@Override
public void onStart() {
super.onStart();
setTitle(mTitle);
updateDisplay();
if (showSettings) {
mAddDirectoryFAB = (FloatingActionButton) getActivity().findViewById(R.id.fab);
if (mAddDirectoryFAB == null) mAddDirectoryFAB = getActivity().findViewById(R.id.fab);
mAddDirectoryFAB.setImageResource(R.drawable.ic_fab_add);
mAddDirectoryFAB.setVisibility(View.VISIBLE);
mAddDirectoryFAB.setOnClickListener(this);
......@@ -194,9 +203,7 @@ public class ExtensionBrowser extends Fragment implements View.OnClickListener,
public boolean onContextItemSelected(MenuItem item) {
ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView
.RecyclerContextMenuInfo) item.getMenuInfo();
if (info != null && handleContextItemSelected(item, info.position))
return true;
return super.onContextItemSelected(item);
return info != null && handleContextItemSelected(item, info.position);
}
public void openContextMenu(final int position) {
......@@ -232,7 +239,7 @@ public class ExtensionBrowser extends Fragment implements View.OnClickListener,
private class ExtensionBrowserHandler extends WeakHandler<ExtensionBrowser> {
public ExtensionBrowserHandler(ExtensionBrowser owner) {
ExtensionBrowserHandler(ExtensionBrowser owner) {
super(owner);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment