|
| 1 | +package com.tcd.gamelauncher; |
| 2 | + |
| 3 | +import android.content.pm.ApplicationInfo; |
| 4 | +import android.content.pm.PackageManager; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.ImageView; |
| 10 | +import android.widget.LinearLayout; |
| 11 | +import android.widget.TextView; |
| 12 | +import androidx.annotation.NonNull; |
| 13 | +import androidx.annotation.Nullable; |
| 14 | +import androidx.fragment.app.Fragment; |
| 15 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 16 | +import androidx.recyclerview.widget.RecyclerView; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class AppSelectorFragment extends Fragment { |
| 20 | + |
| 21 | + private List<ApplicationInfo> filteredApps; |
| 22 | + private PackageManager pm; |
| 23 | + |
| 24 | + public AppSelectorFragment(List<ApplicationInfo> filteredApps, PackageManager pm) { |
| 25 | + this.filteredApps = filteredApps; |
| 26 | + this.pm = pm; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public View onCreateView( |
| 31 | + @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 32 | + return inflater.inflate(R.layout.game_browser, container, false); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 37 | + super.onViewCreated(view, savedInstanceState); |
| 38 | + |
| 39 | + RecyclerView appListView = view.findViewById(R.id.app_selector_recycler_view); |
| 40 | + appListView.setLayoutManager(new LinearLayoutManager(requireContext())); |
| 41 | + |
| 42 | + appListView.setAdapter( |
| 43 | + new RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
| 44 | + @NonNull |
| 45 | + @Override |
| 46 | + public RecyclerView.ViewHolder onCreateViewHolder( |
| 47 | + @NonNull ViewGroup parent, int viewType) { |
| 48 | + View itemView = |
| 49 | + LayoutInflater.from(parent.getContext()) |
| 50 | + .inflate(R.layout.app_selector_item, parent, false); |
| 51 | + return new RecyclerView.ViewHolder(itemView) {}; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
| 56 | + ApplicationInfo appInfo = filteredApps.get(position); |
| 57 | + LinearLayout layout = (LinearLayout) holder.itemView; |
| 58 | + ImageView icon = layout.findViewById(R.id.app_icon); |
| 59 | + TextView name = layout.findViewById(R.id.app_name); |
| 60 | + |
| 61 | + icon.setImageDrawable(appInfo.loadIcon(pm)); |
| 62 | + name.setText(appInfo.loadLabel(pm)); |
| 63 | + |
| 64 | + layout.setOnClickListener( |
| 65 | + v -> { |
| 66 | + if (getActivity() instanceof MainActivity) { |
| 67 | + ((MainActivity) getActivity()) |
| 68 | + .addGame( |
| 69 | + appInfo.packageName, |
| 70 | + appInfo.loadLabel(pm).toString(), |
| 71 | + appInfo.loadIcon(pm)); |
| 72 | + } |
| 73 | + requireActivity().getSupportFragmentManager().popBackStack(); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public int getItemCount() { |
| 79 | + return filteredApps.size(); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + TextView cancelButton = view.findViewById(R.id.Cancel_Btn); |
| 84 | + cancelButton.setOnClickListener( |
| 85 | + v -> { |
| 86 | + requireActivity() |
| 87 | + .getSupportFragmentManager() |
| 88 | + .beginTransaction() |
| 89 | + .setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right) |
| 90 | + .remove(this) |
| 91 | + .commit(); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onResume() { |
| 97 | + super.onResume(); |
| 98 | + ((MainActivity) requireActivity()).setAllowPopupMenu(false); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onPause() { |
| 103 | + super.onPause(); |
| 104 | + ((MainActivity) requireActivity()).setAllowPopupMenu(true); |
| 105 | + } |
| 106 | +} |
0 commit comments