Add an option to open item url in custom tab

This commit is contained in:
Shinokuni 2020-09-06 23:09:59 +02:00
parent e0945823ee
commit c15f093a1b
6 changed files with 41 additions and 9 deletions

View File

@ -71,7 +71,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "androidx.work:work-runtime-ktx:2.4.0" implementation "androidx.work:work-runtime-ktx:2.4.0"
implementation "androidx.fragment:fragment-ktx:1.2.3" implementation "androidx.fragment:fragment-ktx:1.2.3"
implementation "androidx.browser:browser:1.2.0"
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'

View File

@ -22,6 +22,7 @@ import android.webkit.WebView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.ShareCompat; import androidx.core.app.ShareCompat;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
@ -199,12 +200,7 @@ public class ItemActivity extends AppCompatActivity {
shareArticle(); shareArticle();
return true; return true;
case R.id.item_open: case R.id.item_open:
int value = Integer.parseInt(SharedPreferencesManager.readString(this, openUrl();
SharedPreferencesManager.SharedPrefKey.OPEN_ITEMS_IN));
if (value == 0)
openInNavigator();
else
openInWebView();
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -217,6 +213,22 @@ public class ItemActivity extends AppCompatActivity {
super.onBackPressed(); super.onBackPressed();
} }
private void openUrl() {
int value = Integer.parseInt(SharedPreferencesManager.readString(this,
SharedPreferencesManager.SharedPrefKey.OPEN_ITEMS_IN));
switch (value) {
case 0:
openInNavigator();
break;
case 1:
openInWebView();
break;
default:
openInCustomTab();
break;
}
}
private void openInNavigator() { private void openInNavigator() {
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(itemWithFeed.getItem().getLink())); Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(itemWithFeed.getItem().getLink()));
startActivity(urlIntent); startActivity(urlIntent);
@ -225,11 +237,27 @@ public class ItemActivity extends AppCompatActivity {
private void openInWebView() { private void openInWebView() {
Intent intent = new Intent(this, WebViewActivity.class); Intent intent = new Intent(this, WebViewActivity.class);
intent.putExtra(WEB_URL, itemWithFeed.getItem().getLink()); intent.putExtra(WEB_URL, itemWithFeed.getItem().getLink());
intent.putExtra(ACTION_BAR_COLOR, itemWithFeed.getColor() != 0 ? itemWithFeed.getColor() : itemWithFeed.getBgColor()); intent.putExtra(ACTION_BAR_COLOR, itemWithFeed.getBgColor() != 0 ? itemWithFeed.getBgColor() : itemWithFeed.getColor());
startActivity(intent); startActivity(intent);
} }
private void openInCustomTab() {
boolean darkTheme = Boolean.parseBoolean(SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.DARK_THEME));
int color = itemWithFeed.getBgColor() != 0 ? itemWithFeed.getBgColor() : itemWithFeed.getColor();
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.addDefaultShareMenuItem()
.setToolbarColor(color)
.setSecondaryToolbarColor(color)
.setColorScheme(darkTheme ? CustomTabsIntent.COLOR_SCHEME_DARK : CustomTabsIntent.COLOR_SCHEME_LIGHT)
.enableUrlBarHiding()
.setShowTitle(true)
.build();
customTabsIntent.launchUrl(this, Uri.parse(itemWithFeed.getItem().getLink()));
}
private void shareArticle() { private void shareArticle() {
Intent shareIntent = new Intent(Intent.ACTION_SEND); Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain"); shareIntent.setType("text/plain");

View File

@ -132,5 +132,6 @@
<string name="show_caption">Afficher la légende</string> <string name="show_caption">Afficher la légende</string>
<string name="password_helper">Votre mot de passe d\'API (Configuration > Profil)</string> <string name="password_helper">Votre mot de passe d\'API (Configuration > Profil)</string>
<string name="synchronize">Synchroniser</string> <string name="synchronize">Synchroniser</string>
<string name="navigator_view">Vue navigateur</string>
</resources> </resources>

View File

@ -33,11 +33,13 @@
<string-array name="open_items_in"> <string-array name="open_items_in">
<item>@string/external_navigator</item> <item>@string/external_navigator</item>
<item>@string/webview</item> <item>@string/webview</item>
<item>@string/navigator_view</item>
</string-array> </string-array>
<string-array name="open_item_in_values"> <string-array name="open_item_in_values">
<item>0</item> <item>0</item>
<item>1</item> <item>1</item>
<item>2</item>
</string-array> </string-array>
<string-array name="themes"> <string-array name="themes">

View File

@ -138,4 +138,5 @@
<string name="back">Back</string> <string name="back">Back</string>
<string name="show_caption">Show caption</string> <string name="show_caption">Show caption</string>
<string name="synchronize">Synchronize</string> <string name="synchronize">Synchronize</string>
<string name="navigator_view">Navigator view</string>
</resources> </resources>

View File

@ -16,7 +16,7 @@
android:title="@string/reload_feeds_colors" /> android:title="@string/reload_feeds_colors" />
<ListPreference <ListPreference
android:defaultValue="0" android:defaultValue="2"
android:entries="@array/open_items_in" android:entries="@array/open_items_in"
android:entryValues="@array/open_item_in_values" android:entryValues="@array/open_item_in_values"
android:key="open_items_in" android:key="open_items_in"