forked from pockethub/PocketHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlLauncher.java
More file actions
174 lines (144 loc) · 5.67 KB
/
UrlLauncher.java
File metadata and controls
174 lines (144 loc) · 5.67 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright 2012 GitHub Inc.
*
* 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 com.github.mobile.ui;
import static android.content.Intent.ACTION_VIEW;
import static android.content.Intent.CATEGORY_BROWSABLE;
import static org.eclipse.egit.github.core.client.IGitHubConstants.HOST_DEFAULT;
import static org.eclipse.egit.github.core.client.IGitHubConstants.PROTOCOL_HTTPS;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import com.github.mobile.accounts.AccountUtils;
import com.github.mobile.core.commit.CommitMatch;
import com.github.mobile.core.commit.CommitUrlMatcher;
import com.github.mobile.core.gist.GistUrlMatcher;
import com.github.mobile.core.issue.IssueUrlMatcher;
import com.github.mobile.core.user.UserUrlMatcher;
import com.github.mobile.ui.commit.CommitViewActivity;
import com.github.mobile.ui.gist.GistsViewActivity;
import com.github.mobile.ui.issue.IssuesViewActivity;
import com.github.mobile.ui.user.UserViewActivity;
import org.eclipse.egit.github.core.Gist;
import org.eclipse.egit.github.core.Issue;
import org.eclipse.egit.github.core.User;
/**
* Helper to handle any custom activity launching done by selecting URLs
*/
public class UrlLauncher {
private final GistUrlMatcher gistMatcher = new GistUrlMatcher();
private final IssueUrlMatcher issueMatcher = new IssueUrlMatcher();
private final CommitUrlMatcher commitMatcher = new CommitUrlMatcher();
private final UserUrlMatcher userMatcher = new UserUrlMatcher();
private final Context context;
/**
* @param context
*/
public UrlLauncher(final Context context) {
this.context = context;
}
private boolean isValidLogin(final String login) {
return login != null && !login.equals(AccountUtils.getLogin(context));
}
/**
* Create intent to launch view of URI
*
* @param uri
* @return intent
*/
public Intent create(final String uri) {
int issueNumber = issueMatcher.getNumber(uri);
if (issueNumber > 0)
return createIssueIntent(uri, issueNumber);
String gistId = gistMatcher.getId(uri);
if (gistId != null)
return createGistIntent(uri, gistId);
CommitMatch commitMatch = commitMatcher.getCommit(uri);
if (commitMatch != null)
return createCommitIntent(uri, commitMatch);
String login = userMatcher.getLogin(uri);
if (isValidLogin(login))
return createUserIntent(login);
Intent intent = new Intent(ACTION_VIEW, Uri.parse(uri));
intent.addCategory(CATEGORY_BROWSABLE);
return intent;
}
private Intent createUserIntent(final String login) {
return UserViewActivity.createIntent(new User().setLogin(login));
}
private Intent createCommitIntent(final String uri, final CommitMatch match) {
return CommitViewActivity.createIntent(match.repository, match.commit);
}
private Intent createIssueIntent(final String uri, final int number) {
Issue issue = new Issue();
issue.setNumber(number);
issue.setHtmlUrl(uri);
return IssuesViewActivity.createIntent(issue);
}
private Intent createGistIntent(final String uri, final String id) {
Gist gist = new Gist().setId(id).setHtmlUrl(uri);
return GistsViewActivity.createIntent(gist);
}
/**
* Convert global view intent one into one that can be possibly opened
* inside the current application.
*
* @param intent
* @return converted intent or null if non-application specific
*/
public Intent convert(final Intent intent) {
if (intent == null)
return null;
if (!ACTION_VIEW.equals(intent.getAction()))
return null;
Uri data = intent.getData();
if (data == null)
return null;
if (TextUtils.isEmpty(data.getHost())
|| TextUtils.isEmpty(data.getScheme())) {
String host = data.getHost();
if (TextUtils.isEmpty(host))
host = HOST_DEFAULT;
String scheme = data.getScheme();
if (TextUtils.isEmpty(scheme))
scheme = PROTOCOL_HTTPS;
String prefix = scheme + "://" + host;
String path = data.getPath();
if (!TextUtils.isEmpty(path))
if (path.charAt(0) == '/')
data = Uri.parse(prefix + path);
else
data = Uri.parse(prefix + '/' + path);
else
data = Uri.parse(prefix);
intent.setData(data);
}
String uri = data.toString();
int issueNumber = issueMatcher.getNumber(uri);
if (issueNumber > 0)
return createIssueIntent(uri, issueNumber);
String gistId = gistMatcher.getId(uri);
if (gistId != null)
return createGistIntent(uri, gistId);
CommitMatch match = commitMatcher.getCommit(uri);
if (match != null)
return createCommitIntent(uri, match);
String login = userMatcher.getLogin(uri);
if (isValidLogin(login))
return createUserIntent(login);
return null;
}
}