From 379e0f0cadd406c855a0625fd997c7f1c11234bf Mon Sep 17 00:00:00 2001 From: Ilia Kurtov Date: Fri, 4 Aug 2017 14:53:45 +0300 Subject: [PATCH] Base classes for deeplinks processing --- .../deeplinks/ActivityDeepLinkController.java | 75 +++++++++++ .../components/deeplinks/DeepLink.java | 52 ++++++++ .../deeplinks/DeepLinkController.java | 126 ++++++++++++++++++ .../SimpleActivityDeepLinkController.java | 52 ++++++++ 4 files changed, 305 insertions(+) create mode 100644 src/main/java/ru/touchin/roboswag/components/deeplinks/ActivityDeepLinkController.java create mode 100644 src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLink.java create mode 100644 src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLinkController.java create mode 100644 src/main/java/ru/touchin/roboswag/components/deeplinks/SimpleActivityDeepLinkController.java diff --git a/src/main/java/ru/touchin/roboswag/components/deeplinks/ActivityDeepLinkController.java b/src/main/java/ru/touchin/roboswag/components/deeplinks/ActivityDeepLinkController.java new file mode 100644 index 0000000..ac13610 --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/components/deeplinks/ActivityDeepLinkController.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov) + * + * This file is part of RoboSwag library. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package ru.touchin.roboswag.components.deeplinks; + +import android.app.Activity; +import android.net.Uri; +import android.os.Bundle; +import android.support.annotation.NonNull; + +import ru.touchin.roboswag.components.navigation.activities.BaseActivity; + + +/** + * Controller that helps to manage deep links in activity. It helps to save and restore deep link and deletes deep link info from intent. + * As tin he base class - call methods that starts with 'on' prefix from activity. + * + * @param + * @see #onActivityRestoreInstanceState(Bundle) + * @see #onActivitySavedInstanceState(Bundle) + */ +public abstract class ActivityDeepLinkController extends DeepLinkController { + + private static final String DEEP_LINK_EXTRA = "DEEP_LINK_EXTRA"; + + /** + * Call this method on restore instance state - in {@link Activity#onCreate(Bundle)} or in {@link Activity#onRestoreInstanceState(Bundle)}. + * + * @param savedInstanceState - activity's savedInstanceState + */ + public void onActivityRestoreInstanceState(@NonNull final Bundle savedInstanceState) { + final String deepLinkUrl = savedInstanceState.getString(DEEP_LINK_EXTRA, null); + onNewDeepLink(deepLinkUrl == null ? null : Uri.parse(deepLinkUrl)); + } + + /** + * Call this method while saving stat of activity - in {@link Activity#onSaveInstanceState(Bundle)} + * + * @param stateToSave - activity's stateToSave + */ + public void onActivitySavedInstanceState(@NonNull final Bundle stateToSave) { + if (getDeepLinkUri() != null) { + stateToSave.putString(DEEP_LINK_EXTRA, getDeepLinkUri().toString()); + } + } + + /** + * Helps to delete info about deep link from activity's intent and from this controller. + *

+ * Call this after successful deep link processing. + * + * @param activity - that should delete info about processed deep link. + */ + protected void deleteDeepLink(@NonNull final TActivity activity) { + onNewDeepLink(null); + activity.getIntent().setData(null); + } + +} \ No newline at end of file diff --git a/src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLink.java b/src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLink.java new file mode 100644 index 0000000..ae8286b --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLink.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov) + * + * This file is part of RoboSwag library. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package ru.touchin.roboswag.components.deeplinks; + +import android.net.Uri; +import android.support.annotation.NonNull; + +import ru.touchin.roboswag.components.navigation.activities.BaseActivity; + +/** + * Created by Ilia Kurtov on 04.08.2015. + *

+ * Class that helps to operate with deep links. + * + * @param + */ +public interface DeepLink { + + /** + * Called by deep link to provide unique name. + */ + @NonNull + String getName(); + + /** + * Called by deep link to decide - whenever deep link should process uri or if we are already on that screen that deep link links to. + */ + boolean isOnSuchScreen(@NonNull TActivity activity, @NonNull Uri deepLinkUri); + + /** + * Called by deep link to navigate to the specific screen. + */ + void navigateTo(@NonNull TActivity activity, @NonNull Uri deepLinkUri); + +} \ No newline at end of file diff --git a/src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLinkController.java b/src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLinkController.java new file mode 100644 index 0000000..770cf45 --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/components/deeplinks/DeepLinkController.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov) + * + * This file is part of RoboSwag library. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package ru.touchin.roboswag.components.deeplinks; + +import android.app.Activity; +import android.net.Uri; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import ru.touchin.roboswag.components.navigation.activities.BaseActivity; + +/** + * Created by Ilia Kurtov on 04.08.2015. + *

+ * Controller for deep links. Its main goal to decide when deep link should be processed. + *

+ * Call methods that starts with 'on' prefix from {@link TActivity} that should process deep links. + * + * @param Type of Activity to process deep links. + * @see #onNewDeepLink(Uri) + * @see #onActivityReadyToProcessDeepLink(BaseActivity) + * @see #onActivityStopBeingReady() + */ + +public abstract class DeepLinkController { + + @Nullable + private Uri deepLinkUri; + @Nullable + private TActivity activity; + private boolean allowDeepLinkToProcess = true; + + /** + * Get current deep link. + * + * @return - current deep link + */ + @Nullable + protected Uri getDeepLinkUri() { + return deepLinkUri; + } + + /** + * Call this method after receiving new deep link {@link Uri} from your activity. + *

+ * It saves new deepLinkUri and tries to process deep link if possible. + *

+ * In most common cases call this method in {@link Activity#onCreate(Bundle)} if bundle == null or if you want to restore deep link + * in {@link Activity#onCreate(Bundle)} or in {@link Activity#onRestoreInstanceState(Bundle)} methods. + * + * @param deepLinkUri - received deep link. + */ + public void onNewDeepLink(@Nullable final Uri deepLinkUri) { + this.deepLinkUri = deepLinkUri; + startToProcessDeepLinkIfPossible(); + } + + /** + * Call this method when your activity should be ready to process deep link. + *

+ * In most common cases call this method on {@link Activity#onStart()} + * + * @param activity - that should be able to process deep link. + */ + public void onActivityReadyToProcessDeepLink(@NonNull final TActivity activity) { + this.activity = activity; + startToProcessDeepLinkIfPossible(); + } + + /** + * Call this method when your activity stopped being ready to process deep link. + *

+ * In most common cases call this method on {@link Activity#onStop()} + */ + public void onActivityStopBeingReady() { + activity = null; + } + + /** + * This method should be called when you need to add additional condition for processing deep links. By default {@link #allowDeepLinkToProcess} + * equals true. + * + * @param allowDeepLinkToProcess - pass true here if you want to allow deep link to process, otherwise - pass false. + */ + public void setAllowDeepLinkToProcess(final boolean allowDeepLinkToProcess) { + this.allowDeepLinkToProcess = allowDeepLinkToProcess; + startToProcessDeepLinkIfPossible(); + } + + private void startToProcessDeepLinkIfPossible() { + if (activity != null && deepLinkUri != null && allowDeepLinkToProcess) { + processDeepLink(activity, deepLinkUri); + } + } + + /** + * This method would be called if there are non null {@link TActivity}, non null {@link #deepLinkUri} + * and {@link #allowDeepLinkToProcess} equals true. + *

+ * Don't forget to call activity.getIntent().setData(null) after deep link processing + * + * @param activity - that should be able to process deep link. + * @param deepLinkUri - received deep link. + */ + protected abstract void processDeepLink(@NonNull final TActivity activity, + @NonNull final Uri deepLinkUri); + +} \ No newline at end of file diff --git a/src/main/java/ru/touchin/roboswag/components/deeplinks/SimpleActivityDeepLinkController.java b/src/main/java/ru/touchin/roboswag/components/deeplinks/SimpleActivityDeepLinkController.java new file mode 100644 index 0000000..5d9b892 --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/components/deeplinks/SimpleActivityDeepLinkController.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov) + * + * This file is part of RoboSwag library. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package ru.touchin.roboswag.components.deeplinks; + +import android.net.Uri; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import ru.touchin.roboswag.components.navigation.activities.BaseActivity; + +/** + * Created by Ilia Kurtov on 04.08.2015. + *

+ * Simple DeepLinkController that process deep links as it is. When deep links received it would have been processing and navigating id should. + */ +public abstract class SimpleActivityDeepLinkController> + extends ActivityDeepLinkController { + + @Override + protected void processDeepLink(@NonNull final TActivity activity, @NonNull final Uri deepLinkUri) { + deleteDeepLink(activity); + final TDeepLink deepLink = getDeepLinkByUri(deepLinkUri); + if (deepLink != null && !deepLink.isOnSuchScreen(activity, deepLinkUri)) { + deleteDeepLink(activity); + deepLink.navigateTo(activity, deepLinkUri); + } + } + + /** + * Returns deep link that extending {@link DeepLink}. + */ + @Nullable + protected abstract TDeepLink getDeepLinkByUri(@NonNull final Uri deepLinkUri); + +} \ No newline at end of file