Base classes for deeplinks processing

This commit is contained in:
Ilia Kurtov 2017-08-04 14:53:45 +03:00
parent 5fd70d58a7
commit 379e0f0cad
4 changed files with 305 additions and 0 deletions

View File

@ -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 <TActivity>
* @see #onActivityRestoreInstanceState(Bundle)
* @see #onActivitySavedInstanceState(Bundle)
*/
public abstract class ActivityDeepLinkController<TActivity extends BaseActivity> extends DeepLinkController<TActivity> {
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.
* <p>
* 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);
}
}

View File

@ -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.
* <p>
* Class that helps to operate with deep links.
*
* @param <TActivity>
*/
public interface DeepLink<TActivity extends BaseActivity> {
/**
* 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);
}

View File

@ -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.
* <p>
* Controller for deep links. Its main goal to decide when deep link should be processed.
* <p>
* Call methods that starts with 'on' prefix from {@link TActivity} that should process deep links.
*
* @param <TActivity> Type of Activity to process deep links.
* @see #onNewDeepLink(Uri)
* @see #onActivityReadyToProcessDeepLink(BaseActivity)
* @see #onActivityStopBeingReady()
*/
public abstract class DeepLinkController<TActivity extends BaseActivity> {
@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.
* <p>
* It saves new deepLinkUri and tries to process deep link if possible.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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);
}

View File

@ -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.
* <p>
* 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<TActivity extends BaseActivity, TDeepLink extends DeepLink<TActivity>>
extends ActivityDeepLinkController<TActivity> {
@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);
}