This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
forked from saket/Dank
-
Notifications
You must be signed in to change notification settings - Fork 36
/
DeepLinkHandlingActivity.java
89 lines (72 loc) · 3.1 KB
/
DeepLinkHandlingActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package me.saket.dank.deeplinks;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import com.airbnb.deeplinkdispatch.DeepLinkHandler;
import com.airbnb.deeplinkdispatch.DeepLinkResult;
import javax.inject.Inject;
import dagger.Lazy;
import me.saket.dank.di.Dank;
import me.saket.dank.ui.DankActivity;
import me.saket.dank.ui.UrlRouter;
import me.saket.dank.ui.appshortcuts.AppShortcut;
import me.saket.dank.ui.preferences.DefaultWebBrowser;
import me.saket.dank.ui.subreddit.SubredditActivity;
import me.saket.dank.urlparser.Link;
import me.saket.dank.urlparser.RedditSubredditLink;
import me.saket.dank.urlparser.RedditUserLink;
import me.saket.dank.urlparser.UrlParser;
@DeepLinkHandler(AppDeepLinkModule.class)
public class DeepLinkHandlingActivity extends DankActivity {
private static final String KEY_APP_SHORTCUT_ID = "appShortcutId";
@Inject Lazy<UrlParser> urlParser;
@Inject Lazy<UrlRouter> urlRouter;
@Inject Lazy<ShortcutManager> shortcutManager;
public static Intent appShortcutIntent(Context context, AppShortcut shortcut) {
RedditSubredditLink subredditLink = RedditSubredditLink.create(shortcut.label());
return new Intent(Intent.ACTION_VIEW, Uri.parse(subredditLink.unparsedUrl()))
.putExtra(KEY_APP_SHORTCUT_ID, shortcut.id())
.setPackage(context.getPackageName());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// DeepLinkDelegate and AppDeepLinkModuleLoader are generated at compile-time.
DeepLinkDelegate deepLinkDelegate = new DeepLinkDelegate(new AppDeepLinkModuleRegistry());
DeepLinkResult result = deepLinkDelegate.dispatchFrom(this);
if (!result.isSuccessful()) {
Dank.dependencyInjector().inject(this);
if (getIntent().hasExtra(KEY_APP_SHORTCUT_ID)) {
handleAppShortcutDeepLink();
} else {
Uri uri = getIntent().getData();
if (uri != null) {
Link parsedLink = urlParser.get().parse(uri.toString());
if (parsedLink instanceof RedditUserLink) {
Toast.makeText(this, "Viewing user profiles is currently unsupported", Toast.LENGTH_LONG).show();
startActivity(DefaultWebBrowser.DANK_INTERNAL_BROWSER.intentForUrl(this, parsedLink.unparsedUrl(), null));
} else {
urlRouter.get().forLink(parsedLink)
.expandFromBelowToolbar()
.open(this);
}
}
}
}
finish();
}
@TargetApi(Build.VERSION_CODES.N_MR1)
private void handleAppShortcutDeepLink() {
//noinspection ConstantConditions
String subredditUrl = getIntent().getData().toString();
RedditSubredditLink subredditLink = ((RedditSubredditLink) urlParser.get().parse(subredditUrl));
startActivity(SubredditActivity.intent(this, subredditLink));
String shortcutId = getIntent().getStringExtra(KEY_APP_SHORTCUT_ID);
shortcutManager.get().reportShortcutUsed(shortcutId);
}
}