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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ build/
.idea
release.properties
crashlytics.properties
*.iml
17 changes: 9 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ apply plugin: 'android-apt'

dependencies {
apt "org.androidannotations:androidannotations:3.2"
compile "org.androidannotations:androidannotations-api:3.2"
compile 'com.android.support:appcompat-v7:18.0.+'
compile 'com.android.support:support-v4:21.0.+'

compile 'org.androidannotations:androidannotations-api:3.2'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:customtabs:23.3.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
}

android {
compileOptions {
encoding "UTF-8"
}
compileSdkVersion 19
buildToolsVersion '19.1.0'
compileSdkVersion 23
buildToolsVersion '23.0.2'
useLibrary 'org.apache.http.legacy'

defaultConfig {
applicationId "com.manuelmaly.hn"
minSdkVersion 8
targetSdkVersion 19
minSdkVersion 15
targetSdkVersion 23
versionCode 25
versionName "1.9.15"
}
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/manuelmaly/hn/ArticleReaderActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.manuelmaly.hn;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
Expand All @@ -24,6 +25,7 @@
import com.manuelmaly.hn.util.FontHelper;
import com.manuelmaly.hn.util.SpotlightActivity;
import com.manuelmaly.hn.util.ViewedUtils;
import com.manuelmaly.hn.util.CustomTabActivityHelper;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
Expand All @@ -33,7 +35,7 @@
import java.net.URLEncoder;

@EActivity(R.layout.article_activity)
public class ArticleReaderActivity extends ActionBarActivity {
public class ArticleReaderActivity extends ActionBarActivity implements CustomTabActivityHelper.CustomTabFallback {

public static final int ACTIVITY_LOGIN = 137;

Expand Down Expand Up @@ -265,6 +267,17 @@ private void setShowRefreshing(boolean showRefreshing) {
}
}

@Override
public void openUri(Activity activity, HNPost post, String overrideHtmlProvider) {
Intent i = new Intent(activity, ArticleReaderActivity_.class);
i.putExtra(ArticleReaderActivity.EXTRA_HNPOST, post);
if (overrideHtmlProvider != null) {
i.putExtra(ArticleReaderActivity.EXTRA_HTMLPROVIDER_OVERRIDE,
overrideHtmlProvider);
}
activity.startActivity(i);
}

private class HNReaderWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Expand Down
24 changes: 10 additions & 14 deletions app/src/main/java/com/manuelmaly/hn/CommentsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Html;
Expand Down Expand Up @@ -48,6 +49,7 @@
import com.manuelmaly.hn.util.FontHelper;
import com.manuelmaly.hn.util.SpotlightActivity;
import com.manuelmaly.hn.util.ViewedUtils;
import com.manuelmaly.hn.util.CustomTabActivityHelper;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
Expand Down Expand Up @@ -144,6 +146,9 @@ public void onClick(View v) {
CommentsActivity.this);
MainActivity.openURLInBrowser(articleURL,
CommentsActivity.this);
} else if (Settings.getHtmlViewer(CommentsActivity.this).equals(
getString(R.string.pref_htmlviewer_chromecustomtabs))) {
MainActivity.openURLInChromeCustomTabs(mPost, null, CommentsActivity.this);
} else {
openArticleReader();
}
Expand Down Expand Up @@ -368,20 +373,11 @@ private void showCommentsSpotlight() {
}

private void openArticleReader() {
Intent intent = new Intent(this, ArticleReaderActivity_.class);
intent.putExtra(CommentsActivity.EXTRA_HNPOST, mPost);
if (getIntent().getStringExtra(
ArticleReaderActivity.EXTRA_HTMLPROVIDER_OVERRIDE) != null) {
intent.putExtra(
ArticleReaderActivity.EXTRA_HTMLPROVIDER_OVERRIDE,
getIntent().getStringExtra(
ArticleReaderActivity.EXTRA_HTMLPROVIDER_OVERRIDE));
}

startActivity(intent);
overridePendingTransition(android.R.anim.fade_in,
android.R.anim.fade_out);
finish();
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
CustomTabActivityHelper.openCustomTab(
this, customTabsIntent, mPost, Settings.getHtmlProvider(CommentsActivity.this),
new ArticleReaderActivity());
}

private void initCommentsHeader() {
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/manuelmaly/hn/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.manuelmaly.hn.task.ITaskFinishedHandler;
import com.manuelmaly.hn.util.FileUtil;
import com.manuelmaly.hn.util.FontHelper;
import com.manuelmaly.hn.util.CustomTabActivityHelper;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
Expand All @@ -19,16 +20,20 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
Expand Down Expand Up @@ -503,6 +508,9 @@ else if (Settings.getHtmlViewer(MainActivity.this).equals(
openURLInBrowser(
getArticleViewURL(getItem(position)),
MainActivity.this);
} else if (Settings.getHtmlViewer(MainActivity.this).equals(
getString(R.string.pref_htmlviewer_chromecustomtabs))) {
openURLInChromeCustomTabs(getItem(position), null, MainActivity.this);
} else {
openPostInApp(getItem(position), null,
MainActivity.this);
Expand Down Expand Up @@ -721,6 +729,22 @@ public static void openURLInBrowser(String url, Activity a) {
a.startActivity(browserIntent);
}

public static void openURLInChromeCustomTabs(HNPost post, String overrideHtmlProvider, Activity a) {
Intent commentsIntent = new Intent(a, CommentsActivity_.class);
commentsIntent.putExtra(CommentsActivity.EXTRA_HNPOST, post);
PendingIntent pendingIntent = PendingIntent.getActivity(a, 1, commentsIntent,
PendingIntent.FLAG_CANCEL_CURRENT);

Bitmap icon = BitmapFactory.decodeResource(a.getResources(),
R.drawable.ic_launcher);

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setActionButton(icon, a.getString(R.string.comments), pendingIntent);
CustomTabsIntent customTabsIntent = builder.build();
CustomTabActivityHelper.openCustomTab(
a, customTabsIntent, post, overrideHtmlProvider, new ArticleReaderActivity());
}

public static void openPostInApp(HNPost post, String overrideHtmlProvider,
Activity a) {
Intent i = new Intent(a, ArticleReaderActivity_.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.manuelmaly.hn.util;

// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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.

import android.app.Activity;
import android.net.Uri;
import android.support.customtabs.CustomTabsIntent;

import com.manuelmaly.hn.model.HNPost;

import java.util.List;

/**
* This is a helper class to manage the connection to the Custom Tabs Service.
*/
public class CustomTabActivityHelper {

/**
* Opens the URL on a Custom Tab if possible. Otherwise fallsback to opening it on a WebView.
*
* @param activity The host activity.
* @param customTabsIntent a CustomTabsIntent to be used if Custom Tabs is available.
* @param post The HNPost article that must be opened
* @param overrideHtmlProvider something something HTML
* @param fallback a CustomTabFallback to be used if Custom Tabs is not available.
*/
public static void openCustomTab(Activity activity,
CustomTabsIntent customTabsIntent,
HNPost post,
String overrideHtmlProvider,
CustomTabFallback fallback) {
String packageName = CustomTabsHelper.getPackageNameToUse(activity);

// If we cant find a package name, it means theres no browser that supports
// Chrome Custom Tabs installed. So, we fallback to the webview
if (packageName == null) {
if (fallback != null) {
fallback.openUri(activity, post, overrideHtmlProvider);
}
} else {
customTabsIntent.intent.setPackage(packageName);
customTabsIntent.launchUrl(activity, Uri.parse(post.getURL()));
}
}

/**
* To be used as a fallback to open the Uri when Custom Tabs is not available.
*/
public interface CustomTabFallback {
/**
*
* @param activity The Activity that wants to open the Uri.
* @param post The HNPost article that must be opened
* @param overrideHtmlProvider something something HTML
*/
void openUri(Activity activity, HNPost post, String overrideHtmlProvider);
}
}
Loading