/
AppLinkHelper.java
162 lines (135 loc) · 3.6 KB
/
AppLinkHelper.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package org.dolphinemu.dolphinemu.utils;
import android.net.Uri;
import androidx.annotation.StringDef;
import org.dolphinemu.dolphinemu.ui.platform.Platform;
import java.util.List;
/**
* Helps link home screen selection to a game.
*/
public class AppLinkHelper
{
public static final String PLAY = "play";
public static final String BROWSE = "browse";
private static final String SCHEMA_URI_PREFIX = "dolphinemu://app/";
private static final String URI_PLAY = SCHEMA_URI_PREFIX + PLAY;
private static final String URI_VIEW = SCHEMA_URI_PREFIX + BROWSE;
private static final int URI_INDEX_OPTION = 0;
private static final int URI_INDEX_CHANNEL = 1;
private static final int URI_INDEX_GAME = 2;
public static Uri buildGameUri(long channelId, String gameId)
{
return Uri.parse(URI_PLAY)
.buildUpon()
.appendPath(String.valueOf(channelId))
.appendPath(String.valueOf(gameId))
.build();
}
public static Uri buildBrowseUri(Platform platform)
{
return Uri.parse(URI_VIEW).buildUpon().appendPath(platform.getIdString()).build();
}
public static AppLinkAction extractAction(Uri uri)
{
if (isGameUri(uri))
return new PlayAction(extractChannelId(uri), extractGameId(uri));
else if (isBrowseUri(uri))
return new BrowseAction(extractSubscriptionName(uri));
throw new IllegalArgumentException("No action found for uri " + uri);
}
private static boolean isGameUri(Uri uri)
{
if (uri.getPathSegments().isEmpty())
{
return false;
}
String option = uri.getPathSegments().get(URI_INDEX_OPTION);
return PLAY.equals(option);
}
private static boolean isBrowseUri(Uri uri)
{
if (uri.getPathSegments().isEmpty())
return false;
String option = uri.getPathSegments().get(URI_INDEX_OPTION);
return BROWSE.equals(option);
}
private static String extractSubscriptionName(Uri uri)
{
return extract(uri, URI_INDEX_CHANNEL);
}
private static long extractChannelId(Uri uri)
{
return extractLong(uri, URI_INDEX_CHANNEL);
}
private static String extractGameId(Uri uri)
{
return extract(uri, URI_INDEX_GAME);
}
private static long extractLong(Uri uri, int index)
{
return Long.parseLong(extract(uri, index));
}
private static String extract(Uri uri, int index)
{
List<String> pathSegments = uri.getPathSegments();
if (pathSegments.isEmpty() || pathSegments.size() < index)
return null;
return pathSegments.get(index);
}
@StringDef({BROWSE, PLAY})
public @interface ActionFlags
{
}
/**
* Action for deep linking.
*/
public interface AppLinkAction
{
/**
* Returns an string representation of the action.
*/
@ActionFlags
String getAction();
}
/**
* Action when clicking the channel icon
*/
public static class BrowseAction implements AppLinkAction
{
private final String mSubscriptionName;
private BrowseAction(String subscriptionName)
{
this.mSubscriptionName = subscriptionName;
}
@Override
public String getAction()
{
return BROWSE;
}
}
/**
* Action when clicking a program(game)
*/
public static class PlayAction implements AppLinkAction
{
private final long channelId;
private final String gameId;
private PlayAction(long channelId, String gameId)
{
this.channelId = channelId;
this.gameId = gameId;
}
public long getChannelId()
{
return channelId;
}
public String getGameId()
{
return gameId;
}
@Override
public String getAction()
{
return PLAY;
}
}
}