Merge pull request #6394 from TacoTheDank/androidx-fragment-134

Update AndroidX Fragment to 1.3.4
This commit is contained in:
Stypox 2021-06-18 13:14:30 +02:00 committed by GitHub
commit 74ad488f4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 90 additions and 91 deletions

View File

@ -198,6 +198,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.documentfile:documentfile:1.0.1' implementation 'androidx.documentfile:documentfile:1.0.1'
implementation 'androidx.fragment:fragment-ktx:1.3.4'
implementation "androidx.lifecycle:lifecycle-livedata:${androidxLifecycleVersion}" implementation "androidx.lifecycle:lifecycle-livedata:${androidxLifecycleVersion}"
implementation "androidx.lifecycle:lifecycle-viewmodel:${androidxLifecycleVersion}" implementation "androidx.lifecycle:lifecycle-viewmodel:${androidxLifecycleVersion}"
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'

View File

@ -603,6 +603,7 @@ public class MainActivity extends AppCompatActivity {
public void onRequestPermissionsResult(final int requestCode, public void onRequestPermissionsResult(final int requestCode,
@NonNull final String[] permissions, @NonNull final String[] permissions,
@NonNull final int[] grantResults) { @NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for (final int i : grantResults) { for (final int i : grantResults) {
if (i == PackageManager.PERMISSION_DENIED) { if (i == PackageManager.PERMISSION_DENIED) {
return; return;

View File

@ -600,6 +600,7 @@ public class RouterActivity extends AppCompatActivity {
public void onRequestPermissionsResult(final int requestCode, public void onRequestPermissionsResult(final int requestCode,
@NonNull final String[] permissions, @NonNull final String[] permissions,
@NonNull final int[] grantResults) { @NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for (final int i : grantResults) { for (final int i : grantResults) {
if (i == PackageManager.PERMISSION_DENIED) { if (i == PackageManager.PERMISSION_DENIED) {
finish(); finish();

View File

@ -23,13 +23,9 @@ public class ImportConfirmationDialog extends DialogFragment {
public static void show(@NonNull final Fragment fragment, public static void show(@NonNull final Fragment fragment,
@NonNull final Intent resultServiceIntent) { @NonNull final Intent resultServiceIntent) {
if (fragment.getFragmentManager() == null) {
return;
}
final ImportConfirmationDialog confirmationDialog = new ImportConfirmationDialog(); final ImportConfirmationDialog confirmationDialog = new ImportConfirmationDialog();
confirmationDialog.setResultServiceIntent(resultServiceIntent); confirmationDialog.setResultServiceIntent(resultServiceIntent);
confirmationDialog.show(fragment.getFragmentManager(), null); confirmationDialog.show(fragment.getParentFragmentManager(), null);
} }
public void setResultServiceIntent(final Intent resultServiceIntent) { public void setResultServiceIntent(final Intent resultServiceIntent) {

View File

@ -15,6 +15,8 @@ import android.view.MenuInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
@ -82,6 +84,11 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
private lateinit var feedGroupsSortMenuItem: HeaderWithMenuItem private lateinit var feedGroupsSortMenuItem: HeaderWithMenuItem
private val subscriptionsSection = Section() private val subscriptionsSection = Section()
private val requestExportLauncher =
registerForActivityResult(StartActivityForResult(), this::requestExportResult)
private val requestImportLauncher =
registerForActivityResult(StartActivityForResult(), this::requestImportResult)
@State @State
@JvmField @JvmField
var itemsListState: Parcelable? = null var itemsListState: Parcelable? = null
@ -184,39 +191,39 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
} }
private fun onImportPreviousSelected() { private fun onImportPreviousSelected() {
startActivityForResult(StoredFileHelper.getPicker(activity), REQUEST_IMPORT_CODE) requestImportLauncher.launch(StoredFileHelper.getPicker(activity))
} }
private fun onExportSelected() { private fun onExportSelected() {
val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date()) val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date())
val exportName = "newpipe_subscriptions_$date.json" val exportName = "newpipe_subscriptions_$date.json"
startActivityForResult( requestExportLauncher.launch(
StoredFileHelper.getNewPicker(activity, exportName, "application/json", null), StoredFileHelper.getNewPicker(activity, exportName, "application/json", null)
REQUEST_EXPORT_CODE
) )
} }
private fun openReorderDialog() { private fun openReorderDialog() {
FeedGroupReorderDialog().show(requireFragmentManager(), null) FeedGroupReorderDialog().show(parentFragmentManager, null)
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { fun requestExportResult(result: ActivityResult) {
super.onActivityResult(requestCode, resultCode, data) if (result.data != null && result.resultCode == Activity.RESULT_OK) {
if (data != null && data.data != null && resultCode == Activity.RESULT_OK) { activity.startService(
if (requestCode == REQUEST_EXPORT_CODE) { Intent(activity, SubscriptionsExportService::class.java)
activity.startService( .putExtra(SubscriptionsExportService.KEY_FILE_PATH, result.data?.data)
Intent(activity, SubscriptionsExportService::class.java) )
.putExtra(SubscriptionsExportService.KEY_FILE_PATH, data.data) }
) }
} else if (requestCode == REQUEST_IMPORT_CODE) {
ImportConfirmationDialog.show( fun requestImportResult(result: ActivityResult) {
this, if (result.data != null && result.resultCode == Activity.RESULT_OK) {
Intent(activity, SubscriptionsImportService::class.java) ImportConfirmationDialog.show(
.putExtra(KEY_MODE, PREVIOUS_EXPORT_MODE) this,
.putExtra(KEY_VALUE, data.data) Intent(activity, SubscriptionsImportService::class.java)
) .putExtra(KEY_MODE, PREVIOUS_EXPORT_MODE)
} .putExtra(KEY_VALUE, result.data?.data)
)
} }
} }
@ -437,9 +444,4 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
val minWidth = resources.getDimensionPixelSize(R.dimen.channel_item_grid_min_width) val minWidth = resources.getDimensionPixelSize(R.dimen.channel_item_grid_min_width)
return max(1, floor(resources.displayMetrics.widthPixels / minWidth.toDouble()).toInt()) return max(1, floor(resources.displayMetrics.widthPixels / minWidth.toDouble()).toInt())
} }
companion object {
private const val REQUEST_EXPORT_CODE = 666
private const val REQUEST_IMPORT_CODE = 667
}
} }

View File

@ -12,6 +12,9 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
@ -27,6 +30,7 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService; import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService;
import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.ServiceHelper;
@ -34,7 +38,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import icepick.State; import icepick.State;
import org.schabi.newpipe.streams.io.StoredFileHelper;
import static org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.ContentSource.CHANNEL_URL; import static org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.ContentSource.CHANNEL_URL;
import static org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.CHANNEL_URL_MODE; import static org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.CHANNEL_URL_MODE;
@ -43,8 +46,6 @@ import static org.schabi.newpipe.local.subscription.services.SubscriptionsImport
import static org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE; import static org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE;
public class SubscriptionsImportFragment extends BaseFragment { public class SubscriptionsImportFragment extends BaseFragment {
private static final int REQUEST_IMPORT_FILE_CODE = 666;
@State @State
int currentServiceId = Constants.NO_SERVICE_ID; int currentServiceId = Constants.NO_SERVICE_ID;
@ -62,6 +63,9 @@ public class SubscriptionsImportFragment extends BaseFragment {
private EditText inputText; private EditText inputText;
private Button inputButton; private Button inputButton;
private final ActivityResultLauncher<Intent> requestImportFileLauncher =
registerForActivityResult(new StartActivityForResult(), this::requestImportFileResult);
public static SubscriptionsImportFragment getInstance(final int serviceId) { public static SubscriptionsImportFragment getInstance(final int serviceId) {
final SubscriptionsImportFragment instance = new SubscriptionsImportFragment(); final SubscriptionsImportFragment instance = new SubscriptionsImportFragment();
instance.setInitialData(serviceId); instance.setInitialData(serviceId);
@ -173,22 +177,19 @@ public class SubscriptionsImportFragment extends BaseFragment {
} }
public void onImportFile() { public void onImportFile() {
startActivityForResult(StoredFileHelper.getPicker(activity), REQUEST_IMPORT_FILE_CODE); requestImportFileLauncher.launch(StoredFileHelper.getPicker(activity));
} }
@Override private void requestImportFileResult(final ActivityResult result) {
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { if (result.getData() == null) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) {
return; return;
} }
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMPORT_FILE_CODE if (result.getResultCode() == Activity.RESULT_OK && result.getData().getData() != null) {
&& data.getData() != null) {
ImportConfirmationDialog.show(this, ImportConfirmationDialog.show(this,
new Intent(activity, SubscriptionsImportService.class) new Intent(activity, SubscriptionsImportService.class)
.putExtra(KEY_MODE, INPUT_STREAM_MODE) .putExtra(KEY_MODE, INPUT_STREAM_MODE)
.putExtra(KEY_VALUE, data.getData()) .putExtra(KEY_VALUE, result.getData().getData())
.putExtra(Constants.KEY_SERVICE_ID, currentServiceId)); .putExtra(Constants.KEY_SERVICE_ID, currentServiceId));
} }
} }

View File

@ -2,7 +2,6 @@ package org.schabi.newpipe.settings;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
@ -10,6 +9,9 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -40,8 +42,6 @@ import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
public class ContentSettingsFragment extends BasePreferenceFragment { public class ContentSettingsFragment extends BasePreferenceFragment {
private static final int REQUEST_IMPORT_PATH = 8945;
private static final int REQUEST_EXPORT_PATH = 30945;
private static final String ZIP_MIME_TYPE = "application/zip"; private static final String ZIP_MIME_TYPE = "application/zip";
private static final SimpleDateFormat EXPORT_DATE_FORMAT private static final SimpleDateFormat EXPORT_DATE_FORMAT
= new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
@ -56,6 +56,10 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
private Localization initialSelectedLocalization; private Localization initialSelectedLocalization;
private ContentCountry initialSelectedContentCountry; private ContentCountry initialSelectedContentCountry;
private String initialLanguage; private String initialLanguage;
private final ActivityResultLauncher<Intent> requestImportPathLauncher =
registerForActivityResult(new StartActivityForResult(), this::requestImportPathResult);
private final ActivityResultLauncher<Intent> requestExportPathLauncher =
registerForActivityResult(new StartActivityForResult(), this::requestExportPathResult);
@Override @Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
@ -72,20 +76,18 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
final Preference importDataPreference = requirePreference(R.string.import_data); final Preference importDataPreference = requirePreference(R.string.import_data);
importDataPreference.setOnPreferenceClickListener((Preference p) -> { importDataPreference.setOnPreferenceClickListener((Preference p) -> {
startActivityForResult( requestImportPathLauncher.launch(
StoredFileHelper.getPicker(requireContext(), getImportExportDataUri()), StoredFileHelper.getPicker(requireContext(), getImportExportDataUri()));
REQUEST_IMPORT_PATH);
return true; return true;
}); });
final Preference exportDataPreference = requirePreference(R.string.export_data); final Preference exportDataPreference = requirePreference(R.string.export_data);
exportDataPreference.setOnPreferenceClickListener((final Preference p) -> { exportDataPreference.setOnPreferenceClickListener((final Preference p) -> {
startActivityForResult( requestExportPathLauncher.launch(
StoredFileHelper.getNewPicker(requireContext(), StoredFileHelper.getNewPicker(requireContext(),
"NewPipeData-" + EXPORT_DATE_FORMAT.format(new Date()) + ".zip", "NewPipeData-" + EXPORT_DATE_FORMAT.format(new Date()) + ".zip",
ZIP_MIME_TYPE, getImportExportDataUri()), ZIP_MIME_TYPE, getImportExportDataUri()));
REQUEST_EXPORT_PATH);
return true; return true;
}); });
@ -157,37 +159,34 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
} }
} }
@Override private void requestExportPathResult(final ActivityResult result) {
public void onActivityResult(final int requestCode,
final int resultCode,
@Nullable final Intent data) {
assureCorrectAppLanguage(getContext()); assureCorrectAppLanguage(getContext());
super.onActivityResult(requestCode, resultCode, data); if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
if (DEBUG) { lastImportExportDataUri = result.getData().getData(); // will be saved only on success
Log.d(TAG, "onActivityResult() called with: "
+ "requestCode = [" + requestCode + "], "
+ "resultCode = [" + resultCode + "], "
+ "data = [" + data + "]");
}
if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH)
&& resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
lastImportExportDataUri = data.getData(); // will be saved only on success
final StoredFileHelper file final StoredFileHelper file
= new StoredFileHelper(getContext(), data.getData(), ZIP_MIME_TYPE); = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);
if (requestCode == REQUEST_EXPORT_PATH) {
exportDatabase(file); exportDatabase(file);
} else { }
final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); }
builder.setMessage(R.string.override_current_data)
.setPositiveButton(R.string.finish, private void requestImportPathResult(final ActivityResult result) {
(DialogInterface d, int id) -> importDatabase(file)) assureCorrectAppLanguage(getContext());
.setNegativeButton(R.string.cancel, if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
(DialogInterface d, int id) -> d.cancel()); lastImportExportDataUri = result.getData().getData(); // will be saved only on success
builder.create().show();
} final StoredFileHelper file
= new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);
new AlertDialog.Builder(requireActivity())
.setMessage(R.string.override_current_data)
.setPositiveButton(R.string.finish, (d, id) ->
importDatabase(file))
.setNegativeButton(R.string.cancel, (d, id) ->
d.cancel())
.create()
.show();
} }
} }

View File

@ -192,13 +192,13 @@ public class ChooseTabsFragment extends Fragment {
final SelectKioskFragment selectKioskFragment = new SelectKioskFragment(); final SelectKioskFragment selectKioskFragment = new SelectKioskFragment();
selectKioskFragment.setOnSelectedListener((serviceId, kioskId, kioskName) -> selectKioskFragment.setOnSelectedListener((serviceId, kioskId, kioskName) ->
addTab(new Tab.KioskTab(serviceId, kioskId))); addTab(new Tab.KioskTab(serviceId, kioskId)));
selectKioskFragment.show(requireFragmentManager(), "select_kiosk"); selectKioskFragment.show(getParentFragmentManager(), "select_kiosk");
return; return;
case CHANNEL: case CHANNEL:
final SelectChannelFragment selectChannelFragment = new SelectChannelFragment(); final SelectChannelFragment selectChannelFragment = new SelectChannelFragment();
selectChannelFragment.setOnSelectedListener((serviceId, url, name) -> selectChannelFragment.setOnSelectedListener((serviceId, url, name) ->
addTab(new Tab.ChannelTab(serviceId, url, name))); addTab(new Tab.ChannelTab(serviceId, url, name)));
selectChannelFragment.show(requireFragmentManager(), "select_channel"); selectChannelFragment.show(getParentFragmentManager(), "select_channel");
return; return;
case PLAYLIST: case PLAYLIST:
final SelectPlaylistFragment selectPlaylistFragment = new SelectPlaylistFragment(); final SelectPlaylistFragment selectPlaylistFragment = new SelectPlaylistFragment();
@ -215,7 +215,7 @@ public class ChooseTabsFragment extends Fragment {
addTab(new Tab.PlaylistTab(serviceId, url, name)); addTab(new Tab.PlaylistTab(serviceId, url, name));
} }
}); });
selectPlaylistFragment.show(requireFragmentManager(), "select_playlist"); selectPlaylistFragment.show(getParentFragmentManager(), "select_playlist");
return; return;
default: default:
addTab(type.getTab()); addTab(type.getTab());

View File

@ -68,16 +68,14 @@ public enum StreamDialogEntry {
}), // has to be set manually }), // has to be set manually
append_playlist(R.string.append_playlist, (fragment, item) -> { append_playlist(R.string.append_playlist, (fragment, item) -> {
if (fragment.getFragmentManager() != null) { final PlaylistAppendDialog d = PlaylistAppendDialog
final PlaylistAppendDialog d = PlaylistAppendDialog .fromStreamInfoItems(Collections.singletonList(item));
.fromStreamInfoItems(Collections.singletonList(item));
PlaylistAppendDialog.onPlaylistFound(fragment.getContext(), PlaylistAppendDialog.onPlaylistFound(fragment.getContext(),
() -> d.show(fragment.getFragmentManager(), "StreamDialogEntry@append_playlist"), () -> d.show(fragment.getParentFragmentManager(), "StreamDialogEntry@append_playlist"),
() -> PlaylistCreationDialog.newInstance(d) () -> PlaylistCreationDialog.newInstance(d)
.show(fragment.getFragmentManager(), "StreamDialogEntry@create_playlist") .show(fragment.getParentFragmentManager(), "StreamDialogEntry@create_playlist")
); );
}
}), }),
play_with_kodi(R.string.play_with_kodi_title, (fragment, item) -> { play_with_kodi(R.string.play_with_kodi_title, (fragment, item) -> {