33import static org .schabi .newpipe .util .Localization .assureCorrectAppLanguage ;
44
55import android .app .Dialog ;
6- import android .content .Intent ;
76import android .os .Bundle ;
87
98import androidx .annotation .NonNull ;
109import androidx .annotation .Nullable ;
1110import androidx .appcompat .app .AlertDialog ;
1211import androidx .fragment .app .DialogFragment ;
1312import androidx .fragment .app .Fragment ;
13+ import androidx .work .Constraints ;
14+ import androidx .work .Data ;
15+ import androidx .work .ExistingWorkPolicy ;
16+ import androidx .work .NetworkType ;
17+ import androidx .work .OneTimeWorkRequest ;
18+ import androidx .work .OutOfQuotaPolicy ;
19+ import androidx .work .WorkManager ;
1420
1521import com .evernote .android .state .State ;
1622import com .livefront .bridge .Bridge ;
1723
1824import org .schabi .newpipe .R ;
25+ import org .schabi .newpipe .local .subscription .workers .SubscriptionImportWorker ;
26+ import org .schabi .newpipe .util .Constants ;
1927
2028public class ImportConfirmationDialog extends DialogFragment {
2129 @ State
22- protected Intent resultServiceIntent ;
30+ protected int mode ;
31+ @ State
32+ protected String value ;
33+ @ State
34+ protected int serviceId ;
2335
24- public static void show (@ NonNull final Fragment fragment ,
25- @ NonNull final Intent resultServiceIntent ) {
26- final ImportConfirmationDialog confirmationDialog = new ImportConfirmationDialog ();
27- confirmationDialog .setResultServiceIntent ( resultServiceIntent );
36+ public static void show (@ NonNull final Fragment fragment , final int mode ,
37+ @ Nullable final String value , final int serviceId ) {
38+ final var confirmationDialog = new ImportConfirmationDialog ();
39+ confirmationDialog .setData ( mode , value , serviceId );
2840 confirmationDialog .show (fragment .getParentFragmentManager (), null );
2941 }
3042
31- public void setResultServiceIntent (final Intent resultServiceIntent ) {
32- this .resultServiceIntent = resultServiceIntent ;
43+ @ SuppressWarnings ("HiddenField" )
44+ public void setData (final int mode , final String value , final int serviceId ) {
45+ this .mode = mode ;
46+ this .value = value ;
47+ this .serviceId = serviceId ;
3348 }
3449
3550 @ NonNull
3651 @ Override
3752 public Dialog onCreateDialog (@ Nullable final Bundle savedInstanceState ) {
38- assureCorrectAppLanguage (getContext ());
39- return new AlertDialog .Builder (requireContext ())
53+ final var context = requireContext ();
54+ assureCorrectAppLanguage (context );
55+ return new AlertDialog .Builder (context )
4056 .setMessage (R .string .import_network_expensive_warning )
4157 .setCancelable (true )
4258 .setNegativeButton (R .string .cancel , null )
4359 .setPositiveButton (R .string .ok , (dialogInterface , i ) -> {
44- if (resultServiceIntent != null && getContext () != null ) {
45- getContext ().startService (resultServiceIntent );
46- }
60+ final var inputData = new Data .Builder ()
61+ .putString (SubscriptionImportWorker .KEY_VALUE , value )
62+ .putInt (SubscriptionImportWorker .KEY_MODE , mode )
63+ .putInt (Constants .KEY_SERVICE_ID , serviceId )
64+ .build ();
65+ final var constraints = new Constraints .Builder ()
66+ .setRequiredNetworkType (NetworkType .CONNECTED )
67+ .build ();
68+
69+ final var req = new OneTimeWorkRequest .Builder (SubscriptionImportWorker .class )
70+ .setInputData (inputData )
71+ .setExpedited (OutOfQuotaPolicy .RUN_AS_NON_EXPEDITED_WORK_REQUEST )
72+ .setConstraints (constraints )
73+ .build ();
74+
75+ WorkManager .getInstance (context )
76+ .enqueueUniqueWork (SubscriptionImportWorker .WORK_NAME ,
77+ ExistingWorkPolicy .APPEND_OR_REPLACE , req );
78+
4779 dismiss ();
4880 })
4981 .create ();
@@ -53,8 +85,8 @@ public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) {
5385 public void onCreate (@ Nullable final Bundle savedInstanceState ) {
5486 super .onCreate (savedInstanceState );
5587
56- if (resultServiceIntent == null ) {
57- throw new IllegalStateException ("Result intent is null " );
88+ if (mode == 0 && value == null && serviceId == 0 ) {
89+ throw new IllegalStateException ("Input data not provided " );
5890 }
5991
6092 Bridge .restoreInstanceState (this , savedInstanceState );
0 commit comments