|
1 | 1 | package app.gwo.wechat.docuiproxy; |
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint; |
3 | 4 | import android.app.AlertDialog; |
4 | 5 | import android.app.Dialog; |
5 | 6 | import android.app.DialogFragment; |
| 7 | +import android.content.BroadcastReceiver; |
6 | 8 | import android.content.Context; |
| 9 | +import android.content.DialogInterface; |
| 10 | +import android.content.Intent; |
| 11 | +import android.content.IntentFilter; |
| 12 | +import android.content.pm.ResolveInfo; |
| 13 | +import android.os.AsyncTask; |
7 | 14 | import android.os.Bundle; |
8 | 15 | import android.view.ContextThemeWrapper; |
9 | 16 | import android.view.LayoutInflater; |
10 | 17 | import android.view.View; |
| 18 | +import android.widget.CheckBox; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
11 | 22 |
|
12 | 23 | import androidx.annotation.NonNull; |
13 | 24 | import androidx.annotation.Nullable; |
| 25 | +import androidx.localbroadcastmanager.content.LocalBroadcastManager; |
| 26 | +import androidx.recyclerview.widget.RecyclerView; |
| 27 | +import app.gwo.wechat.docuiproxy.adapter.CameraChooserAdapter; |
| 28 | +import app.gwo.wechat.docuiproxy.util.IntentUtils; |
| 29 | +import app.gwo.wechat.docuiproxy.util.Settings; |
| 30 | + |
| 31 | +import static app.gwo.wechat.docuiproxy.Constants.EXTRA_DATA; |
| 32 | +import static java.util.Objects.requireNonNull; |
14 | 33 |
|
15 | 34 | public final class CameraChooserDialogFragment extends DialogFragment { |
16 | 35 |
|
| 36 | + @NonNull |
| 37 | + public static CameraChooserDialogFragment newInstance() { |
| 38 | + return new CameraChooserDialogFragment(); |
| 39 | + } |
| 40 | + |
| 41 | + private static final String EXTRA_LIST_DATA = Constants.EXTRA_PREFIX + ".LIST_DATA"; |
| 42 | + |
17 | 43 | private View mContentView; |
| 44 | + private RecyclerView mRecyclerView; |
| 45 | + private CheckBox mCheckBox; |
| 46 | + |
| 47 | + private CameraChooserAdapter mAdapter; |
| 48 | + private List<ResolveInfo> mData; |
| 49 | + |
| 50 | + private QueryCameraAppsTask mQueryTask; |
| 51 | + |
| 52 | + private final BroadcastReceiver mItemClickReceiver = new BroadcastReceiver() { |
| 53 | + @Override |
| 54 | + public void onReceive(@NonNull Context context, @Nullable Intent intent) { |
| 55 | + requireNonNull(intent); |
| 56 | + final ResolveInfo resolveInfo = intent.getParcelableExtra(EXTRA_DATA); |
| 57 | + if (mCheckBox.isChecked()) { |
| 58 | + Settings.getInstance().setPreferredCamera(IntentUtils.toComponent(resolveInfo)); |
| 59 | + } |
| 60 | + ((ProxyCameraActivity) getActivity()) |
| 61 | + .onStartCameraApp(IntentUtils.toComponent(resolveInfo)); |
| 62 | + } |
| 63 | + }; |
18 | 64 |
|
19 | 65 | @NonNull |
20 | 66 | public Context getThemedContext() { |
21 | 67 | return new ContextThemeWrapper(getActivity(), android.R.style.Theme_Material_Dialog); |
22 | 68 | } |
23 | 69 |
|
24 | 70 | @Override |
25 | | - public Dialog onCreateDialog(Bundle savedInstanceState) { |
26 | | - final AlertDialog.Builder builder = new AlertDialog.Builder( |
27 | | - getActivity(), android.R.style.Theme_Material_Dialog); |
| 71 | + public void onCreate(@Nullable Bundle savedInstanceState) { |
| 72 | + super.onCreate(savedInstanceState); |
| 73 | + |
| 74 | + if (savedInstanceState != null) { |
| 75 | + mData = savedInstanceState.getParcelableArrayList(EXTRA_LIST_DATA); |
| 76 | + } |
| 77 | + |
| 78 | + LocalBroadcastManager.getInstance(getActivity()).registerReceiver( |
| 79 | + mItemClickReceiver, |
| 80 | + new IntentFilter(CameraChooserAdapter.ACTION_ITEM_CLICK)); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onDestroy() { |
| 85 | + super.onDestroy(); |
| 86 | + |
| 87 | + LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mItemClickReceiver); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onDestroyView() { |
| 92 | + super.onDestroyView(); |
| 93 | + if (mQueryTask != null && !mQueryTask.isCancelled()) { |
| 94 | + mQueryTask.cancel(true); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onSaveInstanceState(@NonNull Bundle outState) { |
| 100 | + super.onSaveInstanceState(outState); |
| 101 | + if (mData != null) { |
| 102 | + outState.putParcelableArrayList(EXTRA_LIST_DATA, new ArrayList<>(mData)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { |
| 108 | + final AlertDialog.Builder builder = new AlertDialog.Builder(getThemedContext()); |
28 | 109 | builder.setTitle(R.string.camera_chooser_dialog_title); |
29 | 110 | builder.setView(mContentView = onCreateContentView(savedInstanceState)); |
30 | | - return super.onCreateDialog(savedInstanceState); |
| 111 | + builder.setNegativeButton(android.R.string.cancel, null); |
| 112 | + return builder.create(); |
31 | 113 | } |
32 | 114 |
|
| 115 | + @Override |
| 116 | + public void onDismiss(DialogInterface dialog) { |
| 117 | + super.onDismiss(dialog); |
| 118 | + if (getActivity() != null) { |
| 119 | + getActivity().finish(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @NonNull |
33 | 124 | private View onCreateContentView(@Nullable Bundle savedInstanceState) { |
34 | 125 | final View view = LayoutInflater.from(getThemedContext()) |
35 | 126 | .inflate(R.layout.dialog_camera_chooser_content, null); |
36 | 127 |
|
| 128 | + mRecyclerView = view.findViewById(android.R.id.list); |
| 129 | + mRecyclerView.setHasFixedSize(true); |
| 130 | + if (mAdapter == null) { |
| 131 | + mAdapter = new CameraChooserAdapter(); |
| 132 | + } |
| 133 | + mRecyclerView.setAdapter(mAdapter); |
37 | 134 |
|
| 135 | + mCheckBox = view.findViewById(android.R.id.checkbox); |
| 136 | + |
| 137 | + if (mData != null) { |
| 138 | + mAdapter.submitList(mData); |
| 139 | + } else { |
| 140 | + mQueryTask = new QueryCameraAppsTask(); |
| 141 | + mQueryTask.execute(); |
| 142 | + } |
38 | 143 |
|
39 | 144 | return view; |
40 | 145 | } |
41 | 146 |
|
| 147 | + @SuppressLint("StaticFieldLeak") |
| 148 | + private class QueryCameraAppsTask extends AsyncTask<Void, Void, List<ResolveInfo>> { |
| 149 | + |
| 150 | + @Override |
| 151 | + protected List<ResolveInfo> doInBackground(Void... params) { |
| 152 | + return IntentUtils.queryCameraActivities(getActivity()); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + protected void onPostExecute(@NonNull List<ResolveInfo> result) { |
| 157 | + if (mAdapter != null) { |
| 158 | + mAdapter.submitList(result); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
42 | 164 | } |
0 commit comments