Skip to content

Commit

Permalink
Add Android module
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn committed Dec 4, 2017
1 parent e54d825 commit 184ff71
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 165 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
metadata.json
*.zip
*.xcworkspace/
*.xcuserstate
xcuserdata/
build/
/android/dist
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ as a pre-packaged module for Titanium SDK 7.1.0 and later.
## Roadmap

- [x] Find suitable module name
- [ ] Create both modules on the same namespace (right now [Ti.SafariDialog](https://github.com/appcelerator-modules/ti.safaridialog) vs [Ti.ChromeTabs](https://github.com/prashantsaini1/ti-chrometabs))
- [x] Create both modules on the same namespace (right now [Ti.SafariDialog](https://github.com/appcelerator-modules/ti.safaridialog) vs [Ti.ChromeTabs](https://github.com/prashantsaini1/ti-chrometabs))
- [ ] Adjust existing docs from Ti.SafariDialog to support Android as well
- [ ] Write cross-platform example and tests
- [x] Write cross-platform example and tests
- [ ] Pre-package in the SDK, release module

## License
Expand Down
10 changes: 10 additions & 0 deletions android/java-sources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/src/ti/webdialog/Params.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/src/ti/webdialog/TitaniumWebDialogModule.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/src/ti/webdialog/Utils.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/build/generated/java/ti/webdialog/TitaniumWebDialogBootstrap.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/build/generated/r/android/support/compat/R.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/build/generated/r/android/support/design/R.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/build/generated/r/android/support/v7/appcompat/R.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/build/generated/r/android/support/v7/cardview/R.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/build/generated/r/ti/modules/titanium/ui/R.java"
"/Users/hknoechel/Documents/appcelerator_modules/titanium-web-dialog/android/build/generated/r/ti/webdialog/R.java"
2 changes: 0 additions & 2 deletions android/lib/README

This file was deleted.

Binary file added android/lib/custom-tabs.jar
Binary file not shown.
Binary file added android/libs/arm64-v8a/libti.webdialog.so
Binary file not shown.
Binary file added android/libs/armeabi-v7a/libti.webdialog.so
Binary file not shown.
Binary file added android/libs/x86/libti.webdialog.so
Binary file not shown.
8 changes: 4 additions & 4 deletions android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ version: 1.0.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
description: titanium-web-dialog
author: Hans Knoechel
license: Specify your license
copyright: Copyright (c) 2017 by Your Company
author: Prashant Saini
license: Apache 2.0
copyright: Copyright (c) 2017 by Axway Appcelerator & Prashant Saini

# these should not be edited
name: titanium-web-dialog
moduleid: ti.webdialog
guid: 1f19cce1-0ad7-4e25-ab3b-0666f54a0a49
platform: android
minsdk: 7.0.0.v20171128203009
minsdk: 7.0.0
104 changes: 0 additions & 104 deletions android/src/ti/webdialog/ExampleProxy.java

This file was deleted.

9 changes: 9 additions & 0 deletions android/src/ti/webdialog/Params.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ti.webdialog;

public class Params {
public static final String LCAT = "TiChromeDialog";
public static final String SHOW_TITLE = "showTitle";
public static final String URL = "url";
public static final String BAR_COLOR = "barColor";
public static final String FADE_TRANSITION = "fadeTransition";
}
143 changes: 98 additions & 45 deletions android/src/ti/webdialog/TitaniumWebDialogModule.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,65 +1,118 @@
/**
* This file was auto-generated by the Titanium Module SDK helper for Android
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2017 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*
*/
package ti.webdialog;

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

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.customtabs.CustomTabsIntent;
import android.support.customtabs.CustomTabsService;


@Kroll.module(name="TitaniumWebDialog", id="ti.webdialog")
public class TitaniumWebDialogModule extends KrollModule
{
// Standard Debugging variables
private static final String LCAT = "TiWebDialog";

private List<String> getCustomTabBrowsers(Context context, List<ResolveInfo> browsersList) {
List<String> customTabBrowsers = new ArrayList<String>();

for (ResolveInfo info : browsersList) {
String packageName = info.activityInfo.packageName;

Intent intent = new Intent();
intent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
intent.setPackage(packageName);

if (context.getPackageManager().resolveService(intent, 0) != null) {
customTabBrowsers.add(packageName);
}
}

return customTabBrowsers;
}

private void openCustomTab(Context context, List<String> customTabBrowsers, KrollDict options) {
String URL = options.getString(Params.URL);
URL = URL.toLowerCase();

if (!URL.startsWith("http")) {
URL = "http://" + URL;
}

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setShowTitle(Utils.getBool(options, Params.SHOW_TITLE));

int barColor = Utils.getColor(options, Params.BAR_COLOR);
if (barColor != -1) {
builder.setToolbarColor(barColor);
}

// set start and exit animations
if (Utils.getBool(options, Params.FADE_TRANSITION)) {
builder.setStartAnimations(context, android.R.anim.fade_in, android.R.anim.fade_out);
builder.setExitAnimations(context, android.R.anim.fade_in, android.R.anim.fade_out);
}

CustomTabsIntent tabIntent = builder.build();

for(String s:customTabBrowsers) {
tabIntent.intent.setPackage(s);
}

tabIntent.launchUrl(context, Uri.parse(URL));
}

@Kroll.method
public void open(KrollDict options) {
if ((options != null) && options.containsKeyAndNotNull(Params.URL)) {
Context context = TiApplication.getAppCurrentActivity();
List<ResolveInfo> browsersList = Utils.allBrowsers(context);

if (!browsersList.isEmpty()) {
List<String> customTabBrowsers = getCustomTabBrowsers(context, browsersList);

// show supported browsers list or open directly if only 1 supported browser is available
openCustomTab(context, customTabBrowsers, options);
} else {
Log.i(Params.LCAT, "No browsers available in this device.");
}
}
}

// Standard Debugging variables
private static final String LCAT = "TitaniumWebDialogModule";
private static final boolean DBG = TiConfig.LOGD;

// You can define constants with @Kroll.constant, for example:
// @Kroll.constant public static final String EXTERNAL_NAME = value;

public TitaniumWebDialogModule()
{
super();
}

@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
Log.d(LCAT, "inside onAppCreate");
// put module init code that needs to run when the application is created
}

// Methods
@Kroll.method
public String example()
{
Log.d(LCAT, "example called");
return "hello world";
}

// Properties
@Kroll.getProperty
public String getExampleProp()
{
Log.d(LCAT, "get example property");
return "hello world";
}


@Kroll.setProperty
public void setExampleProp(String value) {
Log.d(LCAT, "set example property: " + value);
}

@Kroll.method
public boolean isSupported() {
Context context = TiApplication.getAppCurrentActivity();
List<ResolveInfo> browsersList = Utils.allBrowsers(context);

return !browsersList.isEmpty();
}

@Kroll.method
public void close(KrollDict options) {
Log.e(Params.LCAT, "The \"close\" method is not implemented, yet!");
}

@Kroll.method
public boolean isOpen(KrollDict options) {
Log.e(Params.LCAT, "The \"isOpen\" method is not implemented, yet!");
return false;
}
}

63 changes: 63 additions & 0 deletions android/src/ti/webdialog/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ti.webdialog;

import java.lang.reflect.Field;
import java.util.List;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiConvert;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;

public class Utils {
public static int getR(String path) {
try {
return TiRHelper.getResource(path);

} catch (Exception exc) {
return -1;
}
}

public static int[] getStyleableIntArray(String packageName, String name) {
try {
Field[] fields2 = Class.forName(packageName + ".R$styleable" ).getFields();

for (Field f : fields2) {
if ( f.getName().equals( name ) ) {
int[] ret = (int[])f.get( null );
return ret;
}
}
} catch ( Throwable t ) {}

return null;
}

// return the list of all available & enabled browsers in device
public static List<ResolveInfo> allBrowsers(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.testingurl.com"));
return context.getPackageManager().queryIntentActivities(intent, 0);
}

public static String getString(KrollDict options, String key) {
String value = "" + (options.containsKeyAndNotNull(key) ? options.get(key) : "");
return value.trim();
}

public static boolean getBool(KrollDict options, String key) {
return (Boolean) (options.containsKeyAndNotNull(key) ? options.get(key) : false);
}

public static int getColor(KrollDict options, String key) {
if (options.containsKeyAndNotNull(key)) {
return TiConvert.toColor( (String) options.get(key));

} else {
return getR("color.colorPrimary");
}
}
}
Loading

0 comments on commit 184ff71

Please sign in to comment.