Skip to content

Commit

Permalink
Ephemeral Tabs project examples
Browse files Browse the repository at this point in the history
  • Loading branch information
erikrodriguez-se committed Dec 11, 2024
1 parent 653bfd2 commit fdd9440
Show file tree
Hide file tree
Showing 59 changed files with 1,348 additions and 0 deletions.
2 changes: 2 additions & 0 deletions demos/custom-tabs-ephemeral-with-fallback/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build

6 changes: 6 additions & 0 deletions demos/custom-tabs-ephemeral-with-fallback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Custom Tabs Ephemeral Demo With Fallback

This demo shows how to launch a Custom Tab with ephemeral browsing enabled.
For devices where ephemeral browsing is not yet available, this demo also
implements a fallback method to open a web view that clears cookies, cache,
history and stored file data.
52 changes: 52 additions & 0 deletions demos/custom-tabs-ephemeral-with-fallback/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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.
*/


apply plugin: 'com.android.application'

android {
namespace 'com.google.androidbrowserhelper.demos.customtabsephemeralwithfallback'
compileSdkVersion 35
defaultConfig {
applicationId "com.google.androidbrowserhelper.demos.customtabsephemeralwithfallback"
minSdkVersion 26
targetSdkVersion 35
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
}

dependencies {
implementation project(path: ':androidbrowserhelper')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.activity:activity:1.9.3'
implementation 'androidx.browser:browser:1.9.0-SNAPSHOT'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.annotation:annotation:1.9.1'
implementation 'androidx.constraintlayout:constraintlayout:2.2.0'
}
22 changes: 22 additions & 0 deletions demos/custom-tabs-ephemeral-with-fallback/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.androidbrowserhelper.demos.customtabsephemeralwithfallback">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WebViewActivity"
android:exported="true">
</activity>
</application>
</manifest>

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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.
*/

package com.google.androidbrowserhelper.demos.customtabsephemeralwithfallback;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.browser.customtabs.CustomTabColorSchemeParams;
import androidx.browser.customtabs.CustomTabsClient;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.browser.customtabs.CustomTabsServiceConnection;
import androidx.browser.customtabs.ExperimentalEphemeralBrowsing;

public class MainActivity extends Activity {
private static final String URL = "https://xchrdw.github.io/browsing-data/siteDataTester.html";
private static final String IS_EPHEMERAL_BROWSING_SUPPORTED = "isEphemeralBrowsingSupported";
private static final String EPHEMERAL_BROWSING_SUPPORTED_KEY = "ephemeralBrowsingSupported";

private CustomTabsClient mClient;
private CustomTabsServiceConnection mConnection;

@Override
protected void onStart() {
super.onStart();

mConnection = new CustomTabsServiceConnection() {
@Override
public void onCustomTabsServiceConnected(@NonNull ComponentName name,
@NonNull CustomTabsClient client) {
mClient = client;
mClient.warmup(0);
}

@Override
public void onServiceDisconnected(ComponentName componentName) { }
};

String packageName = CustomTabsClient.getPackageName(this, null);
if (packageName != null) {
CustomTabsClient.bindCustomTabsService(this, packageName, mConnection);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button launchButton = findViewById(R.id.launch);
launchButton.setOnClickListener(view -> {
if (isEphemeralTabSupported()) {
launchEphemeralTab();
} else {
launchFallbackWebView();
}
});
}

@Override
protected void onStop() {
super.onStop();
if (mConnection == null) return;
unbindService(mConnection);
mConnection = null;
}

@OptIn(markerClass = ExperimentalEphemeralBrowsing.class)
private void launchEphemeralTab() {
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.setEphemeralBrowsingEnabled(true)
.setUrlBarHidingEnabled(false)
.setShareState(CustomTabsIntent.SHARE_STATE_OFF)
.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_back_arrow))
.setDefaultColorSchemeParams(new CustomTabColorSchemeParams.Builder()
.setToolbarColor(getColor(R.color.colorPrimary)).build())
.build();
customTabsIntent.launchUrl(this, Uri.parse(URL));
}

private void launchFallbackWebView() {
Intent webIntent = new Intent(this, WebViewActivity.class);
webIntent.putExtra(WebViewActivity.EXTRA_URL, URL);
startActivity(webIntent);
}

private boolean isEphemeralTabSupported() {
if (mClient == null) {
return false;
}

Bundle bundle = mClient.extraCommand(IS_EPHEMERAL_BROWSING_SUPPORTED, null);
return (bundle != null && bundle.containsKey(EPHEMERAL_BROWSING_SUPPORTED_KEY));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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.
*/

package com.google.androidbrowserhelper.demos.customtabsephemeralwithfallback;

import android.content.Context;
import android.util.Log;

import java.io.File;

public class Utils {
private static final String TAG = "Utils";
private static final String APP_WEB_VIEW_DIRECTORY = "app_webview";

public static void clearAppDirectory(Context context) {
File appDirectory = new File(context.getCacheDir().getParent(), APP_WEB_VIEW_DIRECTORY);
if (appDirectory.exists() && appDirectory.isDirectory()) {
delete(appDirectory);
}
}

private static void delete(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File innerFile : files) {
delete(innerFile);
}
}
}

if (!file.delete()) {
Log.d(TAG, "Could not remove file: " + file.getName());
}
}
}
Loading

0 comments on commit fdd9440

Please sign in to comment.