Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@


package com.thirtydegreesray.openhub.mvp.model;

import android.net.Uri;
import android.support.annotation.NonNull;

import com.thirtydegreesray.openhub.util.GitHubHelper;

import java.util.ArrayList;
import java.util.List;

/**
* Created by ThirtyDegreesRay on 2017/10/30 13:49:02
Expand All @@ -16,21 +13,21 @@
public class GitHubName {

private String url;
private String userName ;
private String repoName ;
private String userName;
private String repoName;

public static GitHubName fromUrl(@NonNull String url){
if(!GitHubHelper.isGitHubUrl(url)) return null;
public static GitHubName fromUrl(@NonNull String url) {
if (!GitHubHelper.isGitHubUrl(url)) return null;
GitHubName gitHubName = new GitHubName();
url = url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
gitHubName.url = url;
try{
try {
Uri uri = Uri.parse(url);
ArrayList<String> list = new ArrayList<>(uri.getPathSegments());
List<String> list = uri.getPathSegments();
list.remove("repos");
if(list.size() > 0) gitHubName.userName = list.get(0);
if(list.size() > 1) gitHubName.repoName = list.get(1);
}catch (Exception e){
if (list.size() > 0) gitHubName.userName = list.get(0);
if (list.size() > 1) gitHubName.repoName = list.get(1);
} catch (Exception e) {

}
return gitHubName;
Expand All @@ -44,19 +41,17 @@ public String getRepoName() {
return repoName;
}

public String getReleaseTagName(){
if(!GitHubHelper.isReleaseTagUrl(url)){
public String getReleaseTagName() {
if (!GitHubHelper.isReleaseTagUrl(url)) {
return null;
}
return url.substring(url.lastIndexOf("/") + 1);
}

public String getCommitShaName(){
if(!GitHubHelper.isCommitUrl(url)){
public String getCommitShaName() {
if (!GitHubHelper.isCommitUrl(url)) {
return null;
}
return url.substring(url.lastIndexOf("/") + 1);
}


}
27 changes: 10 additions & 17 deletions app/src/main/java/com/thirtydegreesray/openhub/util/AppOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,19 @@
import android.support.annotation.NonNull;
import android.support.customtabs.CustomTabsIntent;
import android.widget.Toast;

import com.thirtydegreesray.openhub.R;
import com.thirtydegreesray.openhub.http.Downloader;
import com.thirtydegreesray.openhub.mvp.model.GitHubName;
import com.thirtydegreesray.openhub.service.CopyBroadcastReceiver;
import com.thirtydegreesray.openhub.service.ShareBroadcastReceiver;
import com.thirtydegreesray.openhub.ui.activity.CommitDetailActivity;
import com.thirtydegreesray.openhub.ui.activity.IssueDetailActivity;
import com.thirtydegreesray.openhub.ui.activity.ProfileActivity;
import com.thirtydegreesray.openhub.ui.activity.ReleaseInfoActivity;
import com.thirtydegreesray.openhub.ui.activity.ReleasesActivity;
import com.thirtydegreesray.openhub.ui.activity.RepositoryActivity;
import com.thirtydegreesray.openhub.ui.activity.ViewerActivity;
import com.thirtydegreesray.openhub.ui.activity.*;
import es.dmoral.toasty.Toasty;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import es.dmoral.toasty.Toasty;

/**
* Created by ThirtyDegreesRay on 2017/10/30 11:18:57
*/
Expand Down Expand Up @@ -170,19 +162,20 @@ public static void launchUrl(@NonNull Context context, @NonNull Uri uri){
return;
}
GitHubName gitHubName = GitHubName.fromUrl(url);
String userName ;
String repoName ;

if(gitHubName == null){
openInCustomTabsOrBrowser(context, uri.toString());
return;
} else {
userName = gitHubName.getUserName();
repoName = gitHubName.getRepoName();
}

if(GitHubHelper.isUserUrl(url)){
String userName = gitHubName.getUserName();
String repoName = gitHubName.getRepoName();

if (userName == null) {
context.startActivity(new Intent(context, SplashActivity.class));
} else if (GitHubHelper.isUserUrl(url)) {
ProfileActivity.show((Activity) context, userName);
} else if(GitHubHelper.isRepoUrl(url)){
} else if (GitHubHelper.isRepoUrl(url)) {
RepositoryActivity.show(context, userName, repoName);
} else if (GitHubHelper.isIssueUrl(url)) {
IssueDetailActivity.show((Activity) context, url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,25 @@ public class GitHubHelper {
private static final String GITHUB_BASE_URL_PATTERN_STR = "(https://)?(http://)?(www.)?github.com";

public static final Pattern REPO_FULL_NAME_PATTERN =
Pattern.compile("([a-z]|[A-Z]|\\d|-)*/([a-z]|[A-Z]|\\d|-|\\.|_)*");
Pattern.compile("([a-z]|[A-Z]|\\d|-)+/([a-z]|[A-Z]|\\d|-|\\.|_)+");
private static final Pattern USER_PATTERN = Pattern.compile(GITHUB_BASE_URL_PATTERN_STR
+ "/([a-z]|[A-Z]|\\d|-)*(/)?");
+ "/([a-z]|[A-Z]|\\d|-)+(/)?");
private static final Pattern REPO_PATTERN = Pattern.compile(GITHUB_BASE_URL_PATTERN_STR
+ "/([a-z]|[A-Z]|\\d|-)*/([a-z]|[A-Z]|\\d|-|\\.|_)*(/)?");
+ "/([a-z]|[A-Z]|\\d|-)+/([a-z]|[A-Z]|\\d|-|\\.|_)+(/)?");
private static final Pattern ISSUE_PATTERN = Pattern.compile(GITHUB_BASE_URL_PATTERN_STR
+ "/([a-z]|[A-Z]|\\d|-)*/([a-z]|[A-Z]|\\d|-|\\.|_)*/issues/(\\d)*(/)?");
+ "/([a-z]|[A-Z]|\\d|-)+/([a-z]|[A-Z]|\\d|-|\\.|_)+/issues/(\\d)*(/)?");
private static final Pattern RELEASES_PATTERN = Pattern.compile(GITHUB_BASE_URL_PATTERN_STR
+ "/([a-z]|[A-Z]|\\d|-)*/([a-z]|[A-Z]|\\d|-|\\.|_)*/releases(/latest)?(/)?");
+ "/([a-z]|[A-Z]|\\d|-)+/([a-z]|[A-Z]|\\d|-|\\.|_)+/releases(/latest)?(/)?");
private static final Pattern RELEASE_TAG_PATTERN = Pattern.compile(GITHUB_BASE_URL_PATTERN_STR
+ "/([a-z]|[A-Z]|\\d|-)*/([a-z]|[A-Z]|\\d|-|\\.|_)*/releases/tag/([^/])*(/)?");
+ "/([a-z]|[A-Z]|\\d|-)+/([a-z]|[A-Z]|\\d|-|\\.|_)+/releases/tag/([^/])*(/)?");

private static final Pattern COMMIT_PATTERN = Pattern.compile(GITHUB_BASE_URL_PATTERN_STR
+ "/([a-z]|[A-Z]|\\d|-)*/([a-z]|[A-Z]|\\d|-|\\.|_)*/commit(s)?/([a-z]|\\d)*(/)?");
+ "/([a-z]|[A-Z]|\\d|-)+/([a-z]|[A-Z]|\\d|-|\\.|_)+/commit(s)?/([a-z]|\\d)*(/)?");

private static final Pattern GITHUB_URL_PATTERN = Pattern.compile(GITHUB_BASE_URL_PATTERN_STR + "(.)*");

public static boolean isImage(@Nullable String name) {
if (StringUtils.isBlank(name)) return false;
name = name.toLowerCase();
for (String value : IMAGE_EXTENSIONS) {
String extension = MimeTypeMap.getFileExtensionFromUrl(name);
if ((extension != null && value.replace(".", "").equals(extension))
|| name.endsWith(value))
return true;
}
return false;
return checkFileType(name, IMAGE_EXTENSIONS);
}

public static boolean isMarkdown(@Nullable String name) {
Expand All @@ -72,9 +64,13 @@ public static boolean isMarkdown(@Nullable String name) {
}

public static boolean isArchive(@Nullable String name) {
return checkFileType(name, ARCHIVE_EXTENSIONS);
}

private static boolean checkFileType(@Nullable String name, String[] extensions) {
if (StringUtils.isBlank(name)) return false;
name = name.toLowerCase();
for (String value : ARCHIVE_EXTENSIONS) {
for (String value : extensions) {
String extension = MimeTypeMap.getFileExtensionFromUrl(name);
if ((extension != null && value.replace(".", "").equals(extension))
|| name.endsWith(value))
Expand Down