19
19
20
20
package com .bald .uriah .baldphone .activities ;
21
21
22
- import android .animation .ValueAnimator ;
23
22
import android .content .ComponentName ;
24
23
import android .content .Intent ;
25
24
import android .graphics .Point ;
26
- import android .graphics .drawable .Drawable ;
27
25
import android .net .Uri ;
28
26
import android .os .Bundle ;
29
- import android .util .TypedValue ;
30
27
import android .view .View ;
31
28
import android .view .WindowManager ;
32
- import android .widget .ImageView ;
33
- import android .widget .TextView ;
34
- import androidx .constraintlayout .widget .ConstraintLayout ;
35
- import androidx .core .content .ContextCompat ;
29
+ import android .widget .PopupWindow ;
30
+ import androidx .annotation .NonNull ;
36
31
import androidx .recyclerview .widget .GridLayoutManager ;
37
32
import androidx .recyclerview .widget .RecyclerView ;
38
33
import com .bald .uriah .baldphone .R ;
42
37
import com .bald .uriah .baldphone .databases .apps .AppsDatabaseHelper ;
43
38
import com .bald .uriah .baldphone .utils .BDB ;
44
39
import com .bald .uriah .baldphone .utils .BDialog ;
40
+ import com .bald .uriah .baldphone .utils .DropDownRecyclerViewAdapter ;
45
41
import com .bald .uriah .baldphone .utils .S ;
42
+ import com .bumptech .glide .Glide ;
46
43
47
44
import java .util .List ;
45
+ import java .util .Objects ;
48
46
49
47
import static com .bald .uriah .baldphone .adapters .AppsRecyclerViewAdapter .TYPE_HEADER ;
50
48
@@ -55,19 +53,11 @@ public class AppsActivity extends com.bald.uriah.baldphone.activities.BaldActivi
55
53
public static final int UNINSTALL_REQUEST_CODE = 52 ;
56
54
private static final String TAG = AppsActivity .class .getSimpleName ();
57
55
private static final String SELECTED_APP_INDEX = "SELECTED_APP_INDEX" ;
58
- private static final int TIME_FOR_EFFECT = 300 ;
59
- private int lastIndex = -1 ;
60
56
61
57
//"finals"
62
58
private AppsDatabase appsDatabase ;
63
- private Drawable drawablePin , drawableRemPin ;
64
59
private int numberOfAppsInARow ;
65
- private int barHeight ;
66
60
67
- //views
68
- private View bar , pin , open , uninstall ;
69
- private TextView tv_add_or_rem_shortcut ;
70
- private ImageView iv_pin ;
71
61
private RecyclerView recyclerView ;
72
62
73
63
private AppsRecyclerViewAdapter appsRecyclerViewAdapter ;
@@ -79,16 +69,12 @@ protected void onCreate(Bundle savedInstanceState) {
79
69
80
70
AppsDatabaseHelper .updateDB (this );
81
71
82
- drawablePin = ContextCompat .getDrawable (this , R .drawable .add_on_button );
83
- drawableRemPin = ContextCompat .getDrawable (this , R .drawable .remove_on_button );
84
- barHeight = (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , 120f , getResources ().getDisplayMetrics ());
85
-
86
72
appsDatabase = AppsDatabase .getInstance (AppsActivity .this );
87
73
final List <App > appList = appsDatabase .appsDatabaseDao ().getAllOrderedByABC ();
88
74
attachXml ();
89
75
90
76
final boolean modeChoose = MODE_CHOOSE_ONE .equals (getIntent ().getStringExtra (EXTRA_MODE ));
91
- appsRecyclerViewAdapter = new AppsRecyclerViewAdapter (appList , this , modeChoose ? this ::appChosen : this ::changeBar , recyclerView );
77
+ appsRecyclerViewAdapter = new AppsRecyclerViewAdapter (appList , this , modeChoose ? this ::appChosen : this ::showDropDown , recyclerView );
92
78
93
79
final WindowManager windowManager = getWindowManager ();
94
80
final Point point = new Point ();
@@ -110,57 +96,11 @@ public int getSpanSize(int position) {
110
96
}
111
97
});
112
98
recyclerView .setLayoutManager (gridLayoutManager );
113
-
114
- open .setOnClickListener (v -> {
115
- if (appsRecyclerViewAdapter .index != -1 ) {
116
- final ComponentName componentName = ComponentName .unflattenFromString (((App ) appsRecyclerViewAdapter .dataList .get (appsRecyclerViewAdapter .index )).getFlattenComponentName ());
117
- S .startComponentName (v .getContext (), componentName );
118
- }
119
- });
120
- pin .setOnClickListener (v -> {
121
- if (appsRecyclerViewAdapter .index != -1 ) {
122
-
123
- final App app = (App ) appsRecyclerViewAdapter .dataList .get (appsRecyclerViewAdapter .index );
124
- if (app .isPinned ()) {
125
- appsDatabase .appsDatabaseDao ().update (app .getId (), false );
126
- app .setPinned (false );
127
- appsRecyclerViewAdapter .notifyItemChanged (appsRecyclerViewAdapter .index );
128
- iv_pin .setImageDrawable (drawablePin );
129
- tv_add_or_rem_shortcut .setText (R .string .add_shortcut );
130
- } else {
131
- appsDatabase .appsDatabaseDao ().update (app .getId (), true );
132
- app .setPinned (true );
133
- appsRecyclerViewAdapter .notifyItemChanged (appsRecyclerViewAdapter .index );
134
- iv_pin .setImageDrawable (drawableRemPin );
135
- tv_add_or_rem_shortcut .setText (R .string .remove_shortcut );
136
-
137
- }
138
- }
139
- });
140
- uninstall .setOnClickListener (v -> {
141
- final App app = (App ) appsRecyclerViewAdapter .dataList .get (appsRecyclerViewAdapter .index );
142
- BDB .from (this )
143
- .setTitle (getText (R .string .uninstall ) + app .getLabel ())
144
- .setSubText (String .format (getString (R .string .uninstall_subtext ), app .getLabel (), app .getLabel ()))
145
- .setDialogState (BDialog .DialogState .YES_CANCEL )
146
- .setCancelable (true )
147
- .setPositiveButtonListener (params -> {
148
- uninstallApp (app );
149
- return true ;
150
- })
151
- .show ();
152
- });
153
99
recyclerView .setAdapter (appsRecyclerViewAdapter );
154
100
}
155
101
156
102
private void attachXml () {
157
103
recyclerView = findViewById (R .id .rc_apps );
158
- bar = findViewById (R .id .bar );
159
- pin = bar .findViewById (R .id .pin );
160
- open = bar .findViewById (R .id .open );
161
- uninstall = bar .findViewById (R .id .app_uninstall );
162
- iv_pin = findViewById (R .id .iv_pin );
163
- tv_add_or_rem_shortcut = findViewById (R .id .tv_add_or_rem_shortcut );
164
104
}
165
105
166
106
private void uninstallApp (App app ) {
@@ -181,57 +121,89 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
181
121
}
182
122
}
183
123
184
- private void changeBar (int index ) {
185
- if (lastIndex == index )
124
+ private void showDropDown (final int index ) {
125
+ appsRecyclerViewAdapter .index = index ;
126
+ final App app = (App ) appsRecyclerViewAdapter .dataList .get (index );
127
+ final View view = Objects .requireNonNull (recyclerView .getLayoutManager ()).findViewByPosition (index );
128
+ if (view == null )
186
129
return ;
187
- lastIndex = index ;
188
- if (index == -1 ) {
189
- animateBarView (false );
190
- } else {
191
- final App app = (App ) appsRecyclerViewAdapter .dataList .get (index );
192
- bar .setVisibility (View .VISIBLE );
193
- animateBarView (true );
194
- if (index + numberOfAppsInARow >= appsRecyclerViewAdapter .dataList .size ())
195
- recyclerView .scrollToPosition (index );
130
+ S .showDropDownPopup (this , recyclerView .getWidth (), new DropDownRecyclerViewAdapter .DropDownListener () {
131
+ @ Override
132
+ public void onUpdate (DropDownRecyclerViewAdapter .ViewHolder viewHolder , int position , PopupWindow popupWindow ) {
133
+ switch (position ) {
134
+ case 0 :
135
+ viewHolder .pic .setImageResource (R .drawable .delete_on_button );
136
+ viewHolder .text .setText (R .string .uninstall );
137
+ viewHolder .itemView .setOnClickListener (v1 -> {
138
+ BDB .from (AppsActivity .this )
139
+ .setTitle (String .format ("%s %s" , getText (R .string .uninstall ), app .getLabel ()))
140
+ .setSubText (String .format (getString (R .string .uninstall_subtext ), app .getLabel (), app .getLabel ()))
141
+ .addFlag (BDialog .FLAG_YES | BDialog .FLAG_CANCEL )
142
+ .setPositiveButtonListener (params -> {
143
+ uninstallApp (app );
144
+ return true ;
145
+ })
146
+ .show ();
147
+ popupWindow .dismiss ();
148
+ });
149
+ break ;
150
+ case 1 :
151
+ Glide .with (viewHolder .pic ).load (app .getIcon ()).into (viewHolder .pic );
152
+ viewHolder .text .setText (R .string .open );
153
+ viewHolder .itemView .setOnClickListener (v1 -> {
154
+ final ComponentName componentName = ComponentName .unflattenFromString (app .getFlattenComponentName ());
155
+ S .startComponentName (AppsActivity .this , componentName );
156
+ popupWindow .dismiss ();
157
+ });
158
+ break ;
159
+ case 2 :
160
+ viewHolder .pic .setImageResource (app .isPinned () ? R .drawable .remove_on_button : R .drawable .add_on_button );
161
+ viewHolder .text .setText (app .isPinned () ? R .string .remove_shortcut : R .string .add_shortcut );
162
+ viewHolder .itemView .setOnClickListener (v1 -> {
163
+ if (app .isPinned ()) {
164
+ appsDatabase .appsDatabaseDao ().update (app .getId (), false );
165
+ app .setPinned (false );
166
+ appsRecyclerViewAdapter .notifyItemChanged (appsRecyclerViewAdapter .index );
167
+ } else {
168
+ appsDatabase .appsDatabaseDao ().update (app .getId (), true );
169
+ app .setPinned (true );
170
+ appsRecyclerViewAdapter .notifyItemChanged (appsRecyclerViewAdapter .index );
171
+ }
172
+ popupWindow .dismiss ();
173
+ showDropDown (index );
174
+ });
175
+ break ;
176
+ }
177
+ }
196
178
197
- if (app .isPinned ()) {
198
- tv_add_or_rem_shortcut .setText (R .string .remove_shortcut );
199
- iv_pin .setImageDrawable (drawableRemPin );
200
- } else {
201
- iv_pin .setImageDrawable (drawablePin );
202
- tv_add_or_rem_shortcut .setText (R .string .add_shortcut );
179
+ @ Override public int size () {
180
+ return 3 ;
203
181
}
204
- }
182
+
183
+ @ Override public void onDismiss () {
184
+ if (appsRecyclerViewAdapter .lastView != null )
185
+ appsRecyclerViewAdapter .lastView .setClicked (false );
186
+ appsRecyclerViewAdapter .lastView = null ;
187
+ appsRecyclerViewAdapter .index = -1 ;
188
+
189
+ }
190
+ }, view );
191
+
192
+ if (index + numberOfAppsInARow >= appsRecyclerViewAdapter .dataList .size ())
193
+ recyclerView .scrollToPosition (index );
194
+
205
195
}
206
196
207
197
private void appChosen (int index ) {
208
- if (lastIndex == index )
209
- return ;
210
- lastIndex = index ;
211
- if (index == -1 ) {
212
- animateBarView (false );
213
- } else {
198
+ if (index != -1 ) {
214
199
final App app = (App ) appsRecyclerViewAdapter .dataList .get (index );
215
200
setResult (RESULT_OK , new Intent ().setComponent (ComponentName .unflattenFromString (app .getFlattenComponentName ())));
216
201
finish ();
217
202
}
218
203
}
219
204
220
- public void animateBarView (boolean up ) {
221
- final int desiredInt = up ? barHeight : 0 ;
222
- final ValueAnimator animator = ValueAnimator .ofInt (((ConstraintLayout .LayoutParams ) bar .getLayoutParams ()).height , desiredInt );
223
- animator .addUpdateListener (valueAnimator -> {
224
- ConstraintLayout .LayoutParams params = (ConstraintLayout .LayoutParams ) bar .getLayoutParams ();
225
- params .height = (Integer ) valueAnimator .getAnimatedValue ();
226
- bar .setLayoutParams (params );
227
- }
228
- );
229
- animator .setDuration (TIME_FOR_EFFECT );
230
- animator .start ();
231
- }
232
-
233
205
@ Override
234
- protected void onSaveInstanceState (Bundle outState ) {
206
+ protected void onSaveInstanceState (@ NonNull Bundle outState ) {
235
207
super .onSaveInstanceState (outState );
236
208
outState .putInt (SELECTED_APP_INDEX , ((AppsRecyclerViewAdapter ) recyclerView .getAdapter ()).index );
237
209
}
@@ -243,7 +215,8 @@ protected void onRestoreInstanceState(Bundle savedInstanceState) {
243
215
final AppsRecyclerViewAdapter adapter = ((AppsRecyclerViewAdapter ) recyclerView .getAdapter ());
244
216
if (index < adapter .dataList .size () && index > 0 && adapter .dataList .get (index ).type () != TYPE_HEADER ) {
245
217
adapter .index = index ;
246
- changeBar (index );
218
+ recyclerView .getLayoutManager ().scrollToPosition (index );
219
+ recyclerView .post (() -> showDropDown (index ));
247
220
}
248
221
}
249
222
0 commit comments