Last active
February 25, 2018 18:04
-
-
Save prbale/5d611c155e50d19fc2664d655cadc8fe to your computer and use it in GitHub Desktop.
Android Deep Linking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Deep Link Activity : Start --> | |
<activity | |
android:name=".deeplinks.LinkDispatcherActivity" | |
android:alwaysRetainTaskState="true" | |
android:launchMode="singleTask" | |
android:noHistory="true" | |
android:theme="@android:style/Theme.Translucent.NoTitleBar"> | |
<intent-filter> | |
<action android:name="android.intent.action.VIEW"/> | |
<category android:name="android.intent.category.DEFAULT"/> | |
<category android:name="android.intent.category.BROWSABLE"/> | |
<data android:scheme="example-scheme"/> | |
<data android:host="accounts"/> | |
<data android:host="secureinbox"/> | |
<data android:host="profile"/> | |
</intent-filter> | |
</activity> | |
<!-- Deep Link Activity : End --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IntentHelper { | |
companion object { | |
const val EXTRA_SECURE_INBOX_QUERY = "com.bale.deeplinkdemo.deeplinks.intents.Intents.EXTRA_SECURE_INBOX_QUERY" | |
const val EXTRA_PROFILE_QUERY = "com.bale.deeplinkdemo.deeplinks.intents.Intents.EXTRA_PROFILE_QUERY" | |
const val EXTRA_PROFILE_CHOICE = "com.bale.deeplinkdemo.deeplinks.intents.Intents.EXTRA_PROFILE_CHOICE" | |
} | |
fun newAccountsActivityIntent(context: Context): Intent { | |
val intent: Intent = Intent(context, AccountsActivity::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | |
return intent | |
} | |
fun newLoginIntent(context: Context, isDeepLinkFlow: Boolean): Intent { | |
val intent: Intent = Intent(context, LoginActivity::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | |
intent.putExtra("IS_DEEP_LINK_FLOW", isDeepLinkFlow) | |
return intent | |
} | |
fun newSecureInboxActivityIntent(context: Context, query: String): Intent { | |
val intent: Intent = Intent(context, SecureInboxActivity::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | |
intent.putExtra(EXTRA_SECURE_INBOX_QUERY, query) | |
return intent | |
} | |
fun newProfileActivityIntent(context: Context, query: String, choice: Int): Intent { | |
val intent: Intent = Intent(context, ProfileActivity::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | |
intent.putExtra(EXTRA_PROFILE_QUERY, query) | |
intent.putExtra(EXTRA_PROFILE_CHOICE, choice) | |
return intent | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val mMapper = UriToIntentMapper(this, IntentHelper()) | |
try { | |
mMapper.dispatchIntent(intent, true) | |
} | |
catch (iae: IllegalArgumentException) { | |
if (BuildConfig.DEBUG) { | |
Log.e("Deep links", "Invalid URI", iae) | |
} | |
} | |
finally { | |
// Always finish the Activity so that it doesn't stay in our history | |
finish() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lass UriToIntentMapper(private val mContext: Context, private val mIntents: IntentHelper) { | |
fun dispatchIntent(intent: Intent, isLoginRequired: Boolean = true) { | |
val uri = intent.data | |
var dispatchIntent: Intent? = null | |
if (uri == null) throw IllegalArgumentException("Uri cannot be null") | |
val scheme = uri.scheme.toLowerCase() | |
val host = uri.host.toLowerCase() | |
if ("example-scheme" == scheme) { | |
dispatchIntent = mapAppLink(uri) | |
} | |
if (dispatchIntent != null) { | |
if(isLoginRequired) { | |
(mContext as LinkDispatcherActivity).startActivityForResult(mIntents.newLoginIntent(mContext, true), 111) | |
} | |
else | |
mContext.startActivity(dispatchIntent) | |
} | |
} | |
private fun mapAppLink(uri: Uri): Intent? = when (uri.host.toLowerCase()) { | |
"accounts" -> mIntents.newAccountsActivityIntent(mContext) | |
"secureinbox" -> mIntents.newSecureInboxActivityIntent(mContext, | |
uri.getQueryParameter("query")) | |
"profile" -> mIntents.newProfileActivityIntent(mContext, | |
uri.getQueryParameter("query"), | |
Integer.parseInt(uri.getQueryParameter("choice"))) | |
else -> null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment