Skip to content

Commit

Permalink
[Refactor] Migrate fragment menus to MenuProvider
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
  • Loading branch information
MuntashirAkon committed Jul 8, 2024
1 parent 0903261 commit bb8dd18
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public class AppDetailsComponentsFragment extends AppDetailsFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mNeededProperty = requireArguments().getInt(ARG_TYPE);
}

Expand Down Expand Up @@ -138,16 +137,16 @@ public void onRefresh() {
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
if (viewModel != null && !viewModel.isExternalApk() && SelfPermissions.canModifyAppComponentStates(
viewModel.getUserId(), viewModel.getPackageName(), viewModel.isTestOnlyApp())) {
inflater.inflate(R.menu.fragment_app_details_components_actions, menu);
menuInflater.inflate(R.menu.fragment_app_details_components_actions, menu);
mBlockingToggler = menu.findItem(R.id.action_toggle_blocking);
} else inflater.inflate(R.menu.fragment_app_details_refresh_actions, menu);
} else menuInflater.inflate(R.menu.fragment_app_details_refresh_actions, menu);
}

@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
public void onPrepareMenu(@NonNull Menu menu) {
if (viewModel == null || viewModel.isExternalApk()) {
return;
}
Expand All @@ -162,7 +161,7 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh_details) {
refreshDetails();
Expand All @@ -187,7 +186,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
} else if (id == R.id.action_sort_by_tracker_components) { // Components
setSortBy(AppDetailsFragment.SORT_BY_TRACKERS);
item.setChecked(true);
} else return super.onOptionsItemSelected(item);
} else return false;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

Expand All @@ -31,7 +33,7 @@
import io.github.muntashirakon.widget.SwipeRefreshLayout;

public abstract class AppDetailsFragment extends Fragment implements AdvancedSearchView.OnQueryTextListener,
SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout.OnRefreshListener, MenuProvider {
@IntDef(value = {
APP_INFO,
ACTIVITIES,
Expand Down Expand Up @@ -109,7 +111,6 @@ public abstract class AppDetailsFragment extends Fragment implements AdvancedSea
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
activity = (AppDetailsActivity) requireActivity();
viewModel = new ViewModelProvider(activity).get(AppDetailsViewModel.class);
packageManager = activity.getPackageManager();
Expand Down Expand Up @@ -140,6 +141,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
alertView.setEndIconDrawable(com.google.android.material.R.drawable.mtrl_ic_cancel);
alertView.setEndIconContentDescription(R.string.close);
swipeRefresh.setOnChildScrollUpCallback((parent, child) -> recyclerView.canScrollVertically(-1));
requireActivity().addMenuProvider(this, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
}

@CallSuper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ public void onRefresh() {
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.fragment_app_details_refresh_actions, menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh_details) {
refreshDetails();
} else return super.onOptionsItemSelected(item);
return true;
return true;
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import com.google.android.material.card.MaterialCardView;
import com.google.android.material.chip.Chip;
import com.google.android.material.divider.MaterialDivider;
import com.google.android.material.materialswitch.MaterialSwitch;

import java.lang.annotation.Retention;
Expand All @@ -51,7 +50,6 @@
import io.github.muntashirakon.AppManager.self.imagecache.ImageLoader;
import io.github.muntashirakon.AppManager.self.pref.TipsPrefs;
import io.github.muntashirakon.AppManager.settings.Prefs;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.DateUtils;
import io.github.muntashirakon.AppManager.utils.ExUtils;
import io.github.muntashirakon.AppManager.utils.LangUtils;
Expand All @@ -62,6 +60,7 @@
import io.github.muntashirakon.dialog.SearchableItemsDialogBuilder;
import io.github.muntashirakon.dialog.SearchableSingleChoiceDialogBuilder;
import io.github.muntashirakon.dialog.TextInputDropdownDialogBuilder;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.view.ProgressIndicatorCompat;
import io.github.muntashirakon.widget.MaterialAlertView;
import io.github.muntashirakon.widget.RecyclerView;
Expand Down Expand Up @@ -142,7 +141,7 @@ public void onRefresh() {
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
switch (mNeededProperty) {
case APP_OPS:
inflater.inflate(R.menu.fragment_app_details_app_ops_actions, menu);
Expand All @@ -159,7 +158,7 @@ public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflat
}

@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
public void onPrepareMenu(@NonNull Menu menu) {
if (viewModel == null || viewModel.isExternalApk()) {
return;
}
Expand All @@ -170,7 +169,7 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh_details) {
refreshDetails();
Expand Down Expand Up @@ -281,7 +280,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
} else if (id == R.id.action_sort_by_denied_permissions) {
setSortBy(SORT_BY_DENIED_PERMS);
item.setChecked(true);
} else return super.onOptionsItemSelected(item);
} else return false;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.pm.PackageInfoCompat;
import androidx.core.view.MenuProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -165,7 +167,7 @@
import io.github.muntashirakon.io.Paths;
import io.github.muntashirakon.widget.SwipeRefreshLayout;

public class AppInfoFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
public class AppInfoFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, MenuProvider {
public static final String TAG = "AppInfoFragment";

private static final String PACKAGE_NAME_AURORA_STORE = "com.aurora.store";
Expand Down Expand Up @@ -214,7 +216,6 @@ public class AppInfoFragment extends Fragment implements SwipeRefreshLayout.OnRe
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mAppInfoModel = new ViewModelProvider(this).get(AppInfoViewModel.class);
mMainModel = new ViewModelProvider(requireActivity()).get(AppDetailsViewModel.class);
}
Expand Down Expand Up @@ -251,6 +252,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mVersionView = view.findViewById(R.id.version);
mAdapter = new AppInfoRecyclerAdapter(requireContext());
recyclerView.setAdapter(mAdapter);
mActivity.addMenuProvider(this, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
// Set observer
mMainModel.get(AppDetailsFragment.APP_INFO).observe(getViewLifecycleOwner(), appDetailsItems -> {
mLoadedItemCount = 0;
Expand Down Expand Up @@ -341,14 +343,14 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
if (mMainModel != null && !mMainModel.isExternalApk()) {
inflater.inflate(R.menu.fragment_app_info_actions, menu);
}
}

@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
public void onPrepareMenu(@NonNull Menu menu) {
if (mIsExternalApk) return;
boolean isDebuggable;
if (mApplicationInfo != null) {
Expand Down Expand Up @@ -395,7 +397,7 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_refresh_detail) {
refreshDetails();
Expand Down Expand Up @@ -606,7 +608,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
DexOptDialog dialog = DexOptDialog.getInstance(new String[]{mPackageName});
dialog.show(getChildFragmentManager(), DexOptDialog.TAG);
} else UIUtils.displayShortToast(R.string.only_works_in_root_or_adb_mode);
} else return super.onOptionsItemSelected(item);
} else return false;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import androidx.appcompat.widget.PopupMenu;
import androidx.core.os.BundleCompat;
import androidx.core.os.ParcelCompat;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;
import androidx.transition.Transition;
import androidx.transition.TransitionManager;
Expand Down Expand Up @@ -71,7 +73,7 @@
import io.github.rosemoe.sora.widget.SymbolInputView;
import io.github.rosemoe.sora.widget.schemes.EditorColorScheme;

public class CodeEditorFragment extends AndroidFragment {
public class CodeEditorFragment extends AndroidFragment implements MenuProvider {
public static final String ARG_OPTIONS = "options";

public static class Options implements Parcelable {
Expand Down Expand Up @@ -266,12 +268,6 @@ public void handleOnBackPressed() {
}
};

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -465,6 +461,7 @@ public void afterTextChanged(Editable s) {
mPositionButton.setOnClickListener(v -> {
// TODO: 13/9/22 Enable going to custom places
});
requireActivity().addMenuProvider(this, getViewLifecycleOwner(), Lifecycle.State.RESUMED);

// Update live buttons at the start
updateLiveButtons();
Expand Down Expand Up @@ -545,7 +542,7 @@ public void onResume() {
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.activity_code_editor_actions, menu);
mSaveMenu = menu.findItem(R.id.action_save);
mUndoMenu = menu.findItem(R.id.action_undo);
Expand All @@ -556,7 +553,7 @@ public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflat
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_undo) {
if (mEditor != null && mEditor.canUndo()) {
Expand Down Expand Up @@ -591,12 +588,14 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return true;
} else if (id == R.id.action_java_smali_toggle) {
mViewModel.generateJava(mEditor.getText());
return true;
} else if (id == R.id.action_search) {
if (mSearchWidget != null) {
// FIXME: 21/4/23 Ideally, search widget should have cross button to close it.
if (mSearchWidget.getVisibility() == View.VISIBLE) {
hideSearchWidget();
} else showSearchWidget();
return true;
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import androidx.core.content.ContextCompat;
import androidx.core.os.BundleCompat;
import androidx.core.provider.DocumentsContractCompat;
import androidx.core.view.MenuProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

Expand Down Expand Up @@ -88,8 +90,9 @@
import io.github.muntashirakon.widget.RecyclerView;
import io.github.muntashirakon.widget.SwipeRefreshLayout;

public class FmFragment extends Fragment implements SearchView.OnQueryTextListener, SwipeRefreshLayout.OnRefreshListener,
SpeedDialView.OnActionSelectedListener, MultiSelectionActionsView.OnItemSelectedListener {
public class FmFragment extends Fragment implements MenuProvider, SearchView.OnQueryTextListener,
SwipeRefreshLayout.OnRefreshListener, SpeedDialView.OnActionSelectedListener,
MultiSelectionActionsView.OnItemSelectedListener {
public static final String TAG = FmFragment.class.getSimpleName();

public static final String ARG_URI = "uri";
Expand Down Expand Up @@ -151,7 +154,6 @@ public void handleOnBackPressed() {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mModel = new ViewModelProvider(this).get(FmViewModel.class);
}

Expand Down Expand Up @@ -261,6 +263,7 @@ public void onScrolled(@NonNull androidx.recyclerview.widget.RecyclerView recycl
mMultiSelectionView.updateCounter(true);
BatchOpsHandler batchOpsHandler = new BatchOpsHandler(mMultiSelectionView);
mMultiSelectionView.setOnSelectionChangeListener(batchOpsHandler);
mActivity.addMenuProvider(this, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
// Set observer
mModel.getLastUriLiveData().observe(getViewLifecycleOwner(), uri1 -> {
// force disable empty view
Expand Down Expand Up @@ -419,12 +422,12 @@ public void onAttach(@NonNull Context context) {
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.activity_fm_actions, menu);
}

@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
public void onPrepareMenu(@NonNull Menu menu) {
MenuItem pasteMenu = menu.findItem(R.id.action_paste);
if (pasteMenu != null) {
FmTasks.FmTask fmTask = FmTasks.getInstance().peek();
Expand All @@ -433,7 +436,7 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
mModel.reload();
Expand All @@ -447,7 +450,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
} else if (id == R.id.action_storage) {
ThreadUtils.postOnBackgroundThread(() -> {
ArrayMap<String, Uri> storageLocations = StorageUtils.getAllStorageLocations(mActivity);
if (storageLocations.size() == 0) {
if (storageLocations.isEmpty()) {
mActivity.runOnUiThread(() -> {
if (isDetached()) return;
new MaterialAlertDialogBuilder(mActivity)
Expand Down Expand Up @@ -502,7 +505,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
return false;
}

@Override
Expand Down Expand Up @@ -534,7 +537,7 @@ public boolean onActionSelected(@NonNull SpeedDialActionItem actionItem) {
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
List<Path> selectedFiles = mModel.getSelectedItems();
if (selectedFiles.size() == 0) {
if (selectedFiles.isEmpty()) {
// Do nothing on empty list
return false;
}
Expand Down
Loading

0 comments on commit bb8dd18

Please sign in to comment.