Skip to content

Commit ff53af9

Browse files
committed
Add support for opening Gists from http links
1 parent 11e34ac commit ff53af9

File tree

3 files changed

+114
-26
lines changed

3 files changed

+114
-26
lines changed

app/AndroidManifest.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,24 @@
249249

250250
<category android:name="android.intent.category.DEFAULT" />
251251
</intent-filter>
252+
<intent-filter>
253+
<action android:name="android.intent.action.VIEW" />
254+
255+
<data
256+
android:host="gist.github.com"
257+
android:scheme="http" />
258+
259+
<category android:name="android.intent.category.DEFAULT" />
260+
</intent-filter>
261+
<intent-filter>
262+
<action android:name="android.intent.action.VIEW" />
263+
264+
<data
265+
android:host="gist.github.com"
266+
android:scheme="https" />
267+
268+
<category android:name="android.intent.category.DEFAULT" />
269+
</intent-filter>
252270
</activity>
253271
<activity
254272
android:name=".ui.issue.IssueSearchActivity"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012 GitHub Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.mobile.core.gist;
17+
18+
import android.net.Uri;
19+
import android.text.TextUtils;
20+
21+
import java.util.List;
22+
import java.util.regex.Pattern;
23+
24+
import org.eclipse.egit.github.core.Gist;
25+
26+
/**
27+
* Parses a {@link Gist} from a {@link Uri}
28+
*/
29+
public class GistUriMatcher {
30+
31+
private static final Pattern PATTERN = Pattern.compile("[a-f0-9]{20}");
32+
33+
/**
34+
* Parse a {@link Gist} from a non-null {@link Uri}
35+
*
36+
* @param uri
37+
* @return {@link Gist} or null if none found in given {@link Uri}
38+
*/
39+
public static Gist getGist(final Uri uri) {
40+
List<String> segments = uri.getPathSegments();
41+
if (segments == null)
42+
return null;
43+
if (segments.size() != 1)
44+
return null;
45+
46+
String gistId = segments.get(0);
47+
if (TextUtils.isEmpty(gistId))
48+
return null;
49+
50+
if (TextUtils.isDigitsOnly(gistId))
51+
return new Gist().setId(gistId);
52+
53+
if (PATTERN.matcher(gistId).matches())
54+
return new Gist().setId(gistId);
55+
56+
return null;
57+
}
58+
}

app/src/main/java/com/github/mobile/ui/user/UriLauncherActivity.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static android.content.DialogInterface.BUTTON_POSITIVE;
1919
import static android.content.Intent.ACTION_VIEW;
2020
import static android.content.Intent.CATEGORY_BROWSABLE;
21+
import static org.eclipse.egit.github.core.client.IGitHubConstants.HOST_GISTS;
2122
import android.app.AlertDialog;
2223
import android.content.DialogInterface;
2324
import android.content.DialogInterface.OnCancelListener;
@@ -30,17 +31,20 @@
3031
import com.github.mobile.R.string;
3132
import com.github.mobile.core.commit.CommitMatch;
3233
import com.github.mobile.core.commit.CommitUriMatcher;
34+
import com.github.mobile.core.gist.GistUriMatcher;
3335
import com.github.mobile.core.issue.IssueUriMatcher;
3436
import com.github.mobile.core.repo.RepositoryUriMatcher;
3537
import com.github.mobile.core.user.UserUriMatcher;
3638
import com.github.mobile.ui.LightAlertDialog;
3739
import com.github.mobile.ui.commit.CommitViewActivity;
40+
import com.github.mobile.ui.gist.GistsViewActivity;
3841
import com.github.mobile.ui.issue.IssuesViewActivity;
3942
import com.github.mobile.ui.repo.RepositoryViewActivity;
4043

4144
import java.net.URI;
4245
import java.text.MessageFormat;
4346

47+
import org.eclipse.egit.github.core.Gist;
4448
import org.eclipse.egit.github.core.Repository;
4549
import org.eclipse.egit.github.core.RepositoryIssue;
4650
import org.eclipse.egit.github.core.User;
@@ -56,35 +60,43 @@ protected void onCreate(Bundle savedInstanceState) {
5660

5761
final Intent intent = getIntent();
5862
final Uri data = intent.getData();
63+
if (HOST_GISTS.equals(data.getHost())) {
64+
Gist gist = GistUriMatcher.getGist(data);
65+
if (gist != null) {
66+
startActivity(GistsViewActivity.createIntent(gist));
67+
finish();
68+
return;
69+
}
70+
} else {
71+
RepositoryIssue issue = IssueUriMatcher.getIssue(data);
72+
if (issue != null) {
73+
startActivity(IssuesViewActivity.createIntent(issue,
74+
issue.getRepository()));
75+
finish();
76+
return;
77+
}
5978

60-
RepositoryIssue issue = IssueUriMatcher.getIssue(data);
61-
if (issue != null) {
62-
startActivity(IssuesViewActivity.createIntent(issue,
63-
issue.getRepository()));
64-
finish();
65-
return;
66-
}
67-
68-
Repository repository = RepositoryUriMatcher.getRepository(data);
69-
if (repository != null) {
70-
startActivity(RepositoryViewActivity.createIntent(repository));
71-
finish();
72-
return;
73-
}
79+
Repository repository = RepositoryUriMatcher.getRepository(data);
80+
if (repository != null) {
81+
startActivity(RepositoryViewActivity.createIntent(repository));
82+
finish();
83+
return;
84+
}
7485

75-
User user = UserUriMatcher.getUser(data);
76-
if (user != null) {
77-
startActivity(UserViewActivity.createIntent(user));
78-
finish();
79-
return;
80-
}
86+
User user = UserUriMatcher.getUser(data);
87+
if (user != null) {
88+
startActivity(UserViewActivity.createIntent(user));
89+
finish();
90+
return;
91+
}
8192

82-
CommitMatch commit = CommitUriMatcher.getCommit(data);
83-
if (commit != null) {
84-
startActivity(CommitViewActivity.createIntent(commit.repository,
85-
commit.commit));
86-
finish();
87-
return;
93+
CommitMatch commit = CommitUriMatcher.getCommit(data);
94+
if (commit != null) {
95+
startActivity(CommitViewActivity.createIntent(
96+
commit.repository, commit.commit));
97+
finish();
98+
return;
99+
}
88100
}
89101

90102
if (!intent.hasCategory(CATEGORY_BROWSABLE)) {

0 commit comments

Comments
 (0)