Skip to content

Commit

Permalink
Fix launching URLs from the thumbnail / preview row
Browse files Browse the repository at this point in the history
  • Loading branch information
c99koder committed Dec 14, 2017
1 parent 649fe78 commit 4234b48
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ android {
useLibrary 'org.apache.http.legacy'

defaultConfig {
versionCode 166
versionCode 167
versionName "4.5.1"
minSdkVersion 14
targetSdkVersion 27
Expand Down
56 changes: 30 additions & 26 deletions src/com/irccloud/android/IRCCloudLinkMovementMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,7 @@ public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event
if (action == MotionEvent.ACTION_UP) {
Uri uri = Uri.parse(link[0].getURL());
Context context = widget.getContext();
if(!PreferenceManager.getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext()).getBoolean("browser", false) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && uri.getScheme().startsWith("http") && CustomTabsHelper.getPackageNameToUse(context) != null) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(ColorScheme.getInstance().navBarColor);
builder.addDefaultShareMenuItem();
builder.addMenuItem("Copy URL", PendingIntent.getBroadcast(context, 0, new Intent(context, ChromeCopyLinkBroadcastReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT));
CustomTabsIntent intent = builder.build();
intent.intent.setData(uri);
if(Build.VERSION.SDK_INT >= 22)
intent.intent.putExtra(Intent.EXTRA_REFERRER, Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + context.getPackageName()));
if (Build.VERSION.SDK_INT >= 16 && intent.startAnimationBundle != null) {
context.startActivity(intent.intent, intent.startAnimationBundle);
} else {
context.startActivity(intent.intent);
}
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
if(Build.VERSION.SDK_INT >= 22)
intent.putExtra(Intent.EXTRA_REFERRER, Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + context.getPackageName()));
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Unable to find an application to handle this URL scheme", Toast.LENGTH_SHORT).show();
}
}
launchURI(uri, context);
} else {
Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
}
Expand All @@ -102,4 +77,33 @@ public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event
}
return false;
}

public static void launchURI(Uri uri, Context context) {
if(!PreferenceManager.getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext()).getBoolean("browser", false) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && uri.getScheme().startsWith("http") && CustomTabsHelper.getPackageNameToUse(context) != null) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(ColorScheme.getInstance().navBarColor);
builder.addDefaultShareMenuItem();
builder.addMenuItem("Copy URL", PendingIntent.getBroadcast(context, 0, new Intent(context, ChromeCopyLinkBroadcastReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT));
CustomTabsIntent intent = builder.build();
intent.intent.setData(uri);
if(Build.VERSION.SDK_INT >= 22)
intent.intent.putExtra(Intent.EXTRA_REFERRER, Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + context.getPackageName()));
if (Build.VERSION.SDK_INT >= 16 && intent.startAnimationBundle != null) {
context.startActivity(intent.intent, intent.startAnimationBundle);
} else {
context.startActivity(intent.intent);
}
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
if(Build.VERSION.SDK_INT >= 22)
intent.putExtra(Intent.EXTRA_REFERRER, Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + context.getPackageName()));
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Unable to find an application to handle this URL scheme", Toast.LENGTH_SHORT).show();
}
}
}
}
21 changes: 11 additions & 10 deletions src/com/irccloud/android/fragment/MessageViewFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2297,23 +2297,24 @@ public void run() {
}
} else if (e.row_type == ROW_THUMBNAIL) {
try {
Intent intent = new Intent(getActivity(), ImageViewerActivity.class);
String uri;
if(e.entities.has("id"))
intent.setData(Uri.parse(UriTemplate.fromTemplate(ColorFormatter.file_uri_template).set("id", e.entities.get("id").asText()).expand()));
uri = UriTemplate.fromTemplate(ColorFormatter.file_uri_template).set("id", e.entities.get("id").asText()).set("name", e.entities.get("name").asText()).expand();
else
intent.setData(Uri.parse(e.entities.get("url").asText()));
startActivity(intent);
uri = e.entities.get("url").asText();
if (PreferenceManager.getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext()).getBoolean("imageviewer", true)) {
if (uri.toLowerCase().startsWith("http://"))
uri = IRCCloudApplication.getInstance().getApplicationContext().getResources().getString(R.string.IMAGE_SCHEME) + "://" + uri.substring(7);
else if (uri.toLowerCase().startsWith("https://"))
uri = IRCCloudApplication.getInstance().getApplicationContext().getResources().getString(R.string.IMAGE_SCHEME_SECURE) + "://" + uri.substring(8);
}
IRCCloudLinkMovementMethod.launchURI(Uri.parse(uri), getActivity());
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.row_type == ROW_FILE) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(UriTemplate.fromTemplate(ColorFormatter.file_uri_template).set("id", e.entities.get("id").asText()).expand()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, getActivity().getPackageName());
if (Build.VERSION.SDK_INT >= 22)
intent.putExtra(Intent.EXTRA_REFERRER, Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + getActivity().getPackageName()));
startActivity(intent);
IRCCloudLinkMovementMethod.launchURI(Uri.parse(UriTemplate.fromTemplate(ColorFormatter.file_uri_template).set("id", e.entities.get("id").asText()).set("name", e.entities.get("name").asText()).expand()), getActivity());
} catch (ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Unable to find an application to handle this URL scheme", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Expand Down

0 comments on commit 4234b48

Please sign in to comment.