Skip to content

Android: readme, bugfix for configure() #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2018
Merged
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
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ Use the native Firebase SDK in Axway Titanium. This repository is part of the [T

Configure Firebase without additional parameters.

<b>Android</b>: returns false if it was already configured or if there was an error. Calling `deleteInstanceId()` can be used to re-configure it again.

##### `configure(parameters)`

Configure Firebase without configuration parameters.
Configure Firebase with configuration parameters:

| Name | Type | Component | Platform |
| - | - | - | - |
| `file` | String | | *

By passing the `file` property, you can give a location to the Firebase plist file (usually named "GoogleService-Info.plist"), which contains all necessary properties for your Firebase project. This makes all other properties unnecessary. For Android: place the file in `/app/assets/android/` and pass just the filename.

Or you can configure Firebase without a file by passing these parameters:

| Name | Type | Component | Platform |
| - | - | - | - |
| `file`* | String | | *
| `googleAppID` | String | | *
| `GCMSenderID` | String | Cloud Messaging | *
| `APIKey` | String | Auth | *
Expand All @@ -38,16 +47,15 @@ Configure Firebase without configuration parameters.
| `deepLinkURLScheme` | String | | iOS
| `applicationID` | String | Analytics | Android

\* By passing the `file` property, you can give a location to the Firebase plist file (usually named "GoogleService-Info.plist"), which contains all necessary properties for your Firebase project. This makes all other properties unnecessary. For Android: place the file in `/app/assets/android/` and pass just the filename.

##### `deleteInstanceId(callback)`

Delete the current `instanceId` (invalidating all tokens). See the [Firebase docs](https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId.html#deleteInstanceId()) for details.

The callback receives an object containing this fields:

| Key | Type |Description |
| - | - | - |
| Key | Type | Description | Platform |
| - | - | - | - |
| `success` | Boolean | `true` if the deletion succeeded | *
| `error` | String | The localized error message | *

Expand All @@ -57,8 +65,8 @@ Delete the token of the provided `authorizedEntity` and `scope`. See the [Fireba

The callback receives an object containing this fields:

| Key | Type | Description |
| - | - | - |
| Key | Type | Description | Platform |
| - | - | - | - |
| `success` | Boolean | `true` if the deletion succeeded | *
| `error` | String | The localized error message | *

Expand All @@ -69,6 +77,11 @@ The callback receives an object containing this fields:
var FirebaseCore = require('firebase.core');

// Configure your Firebase API's (only required once for all)

FirebaseCore.configure(); // default google-services.json/GoogleService-Info.plist will be used

// alternative way:

FirebaseCore.configure({
APIKey: "AIYasZBtfJh..........",
projectID: "test-12345",
Expand Down
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.3.0
version: 2.3.1
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
description: titanium-firebase-core
Expand Down
175 changes: 98 additions & 77 deletions android/src/firebase/core/TitaniumFirebaseCoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.InputStream;
import java.lang.Void;
import java.util.HashMap;
import java.util.List;

@Kroll.module(name = "TitaniumFirebaseCore", id = "firebase.core")
public class TitaniumFirebaseCoreModule extends KrollModule
Expand All @@ -44,99 +45,115 @@ public TitaniumFirebaseCoreModule()
// Public APIs

@Kroll.method
public void configure(@Kroll.argument(optional = true) KrollDict param)
public boolean configure(@Kroll.argument(optional = true) KrollDict param)
{
if (param != null) {
String apiKey = "";
String databaseURL = "";
String projectID = "";
String storageBucket = "";
String applicationID = "";
String GCMSenderID = "";
FirebaseOptions.Builder options = new FirebaseOptions.Builder();

if (param.containsKey("file")) {
// open file and parse it
try {
JSONObject json = new JSONObject(loadJSONFromAsset(param.getString("file")));
JSONObject projectInfo = json.getJSONObject("project_info");
String packageName = TiApplication.getAppCurrentActivity().getPackageName();
String filename = null;

if (projectInfo.has("storage_bucket")) {
storageBucket = projectInfo.getString("storage_bucket");
}
if (projectInfo.has("firebase_url")) {
databaseURL = projectInfo.getString("firebase_url");
}
if (projectInfo.has("project_number")) {
GCMSenderID = projectInfo.getString("project_number");
}
if (projectInfo.has("project_id")) {
projectID = projectInfo.getString("project_id");
}
if (json.has("client")) {
JSONArray clients = json.getJSONArray("client");
for (int i = 0, len = clients.length(); i < len; i++) {
JSONObject client = clients.getJSONObject(i);
JSONObject clientInfo = client.getJSONObject("client_info");
String pName = clientInfo.getJSONObject("android_client_info").getString("package_name");
if (pName.equals(packageName)) {
applicationID = client.getJSONObject("client_info").getString("mobilesdk_app_id");
apiKey = client.getJSONArray("api_key").getJSONObject(0).getString("current_key");
}
}
}
} catch (JSONException e) {
Log.e(LCAT, "Error parsing file");
}
} else {
// use parameters
if (param.containsKey("APIKey")) {
apiKey = param.getString("APIKey");
}
if (param.containsKey("databaseURL")) {
databaseURL = param.getString("databaseURL");
if (param == null) {
filename = "google-services.json";
} else if (param.containsKey("file")) {
filename = param.getString("file");
}
String apiKey = "";
String databaseURL = "";
String projectID = "";
String storageBucket = "";
String applicationID = "";
String GCMSenderID = "";
FirebaseOptions.Builder options = new FirebaseOptions.Builder();

if (filename != null) {
// open file and parse it
try {
JSONObject json = new JSONObject(loadJSONFromAsset(filename));
JSONObject projectInfo = json.getJSONObject("project_info");
String packageName = TiApplication.getAppCurrentActivity().getPackageName();

if (projectInfo.has("storage_bucket")) {
storageBucket = projectInfo.getString("storage_bucket");
}
if (param.containsKey("projectID")) {
projectID = param.getString("projectID");
if (projectInfo.has("firebase_url")) {
databaseURL = projectInfo.getString("firebase_url");
}
if (param.containsKey("storageBucket")) {
storageBucket = param.getString("storageBucket");
if (projectInfo.has("project_number")) {
GCMSenderID = projectInfo.getString("project_number");
}
if (param.containsKey("applicationID")) {
applicationID = param.getString("applicationID");
if (projectInfo.has("project_id")) {
projectID = projectInfo.getString("project_id");
}
if (param.containsKey("GCMSenderID")) {
GCMSenderID = param.getString("GCMSenderID");
if (json.has("client")) {
JSONArray clients = json.getJSONArray("client");
for (int i = 0, len = clients.length(); i < len; i++) {
JSONObject client = clients.getJSONObject(i);
JSONObject clientInfo = client.getJSONObject("client_info");
String pName = clientInfo.getJSONObject("android_client_info").getString("package_name");
if (pName.equals(packageName)) {
applicationID = client.getJSONObject("client_info").getString("mobilesdk_app_id");
apiKey = client.getJSONArray("api_key").getJSONObject(0).getString("current_key");
}
}
}
} catch (JSONException e) {
Log.e(LCAT, "Error parsing file");
}
} else {
// use parameters
if (param.containsKey("APIKey")) {
apiKey = param.getString("APIKey");
}
if (param.containsKey("databaseURL")) {
databaseURL = param.getString("databaseURL");
}
if (param.containsKey("projectID")) {
projectID = param.getString("projectID");
}
if (param.containsKey("storageBucket")) {
storageBucket = param.getString("storageBucket");
}
if (param.containsKey("applicationID")) {
applicationID = param.getString("applicationID");
}
if (param.containsKey("GCMSenderID")) {
GCMSenderID = param.getString("GCMSenderID");
}
}

options.setApiKey(apiKey);
options.setDatabaseUrl(databaseURL);
options.setProjectId(projectID);
options.setStorageBucket(storageBucket);
options.setApplicationId(applicationID);
options.setGcmSenderId(GCMSenderID);
options.setApiKey(apiKey);
options.setDatabaseUrl(databaseURL);
options.setProjectId(projectID);
options.setStorageBucket(storageBucket);
options.setApplicationId(applicationID);
options.setGcmSenderId(GCMSenderID);

// check for existing firebaseApp
boolean hasBeenInitialized = false;
List<FirebaseApp> fbsLcl = FirebaseApp.getApps(getActivity().getApplicationContext());
for (FirebaseApp app : fbsLcl) {
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) {
hasBeenInitialized = true;
}
}

if (!hasBeenInitialized) {
try {
FirebaseApp.initializeApp(getActivity().getApplicationContext(), options.build());
return true;
} catch (IllegalStateException e) {
Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time.");
return false;
}
} else {
try {
FirebaseApp.initializeApp(getActivity().getApplicationContext());
} catch (IllegalStateException e) {
Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time.");
}
Log.d(LCAT, "FirebaseApp is alraedy initialized.");
return false;
}
}

@Kroll.method
public void deleteInstanceId(final KrollFunction callback)
{
new AsyncTask<Void, Void, IOException>() {
protected IOException doInBackground(Void ... v) {
protected IOException doInBackground(Void... v)
{
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
return null;
Expand All @@ -145,7 +162,8 @@ protected IOException doInBackground(Void ... v) {
return e;
}
}
protected void onPostExecute(IOException error) {
protected void onPostExecute(IOException error)
{
if (callback != null) {
HashMap args = new HashMap<>();
args.put("success", error == null);
Expand All @@ -155,14 +173,16 @@ protected void onPostExecute(IOException error) {
callback.call(getKrollObject(), args);
}
}
}.execute();
}
.execute();
}

@Kroll.method
public void deleteToken(final String authorizedEntity, final String scope, final KrollFunction callback)
{
new AsyncTask<Void, Void, IOException>() {
protected IOException doInBackground(Void ... v) {
protected IOException doInBackground(Void... v)
{
try {
FirebaseInstanceId.getInstance().deleteToken(authorizedEntity, scope);
return null;
Expand All @@ -171,7 +191,8 @@ protected IOException doInBackground(Void ... v) {
return e;
}
}
protected void onPostExecute(IOException error) {
protected void onPostExecute(IOException error)
{
if (callback != null) {
HashMap args = new HashMap<>();
args.put("success", error == null);
Expand All @@ -181,7 +202,8 @@ protected void onPostExecute(IOException error) {
callback.call(getKrollObject(), args);
}
}
}.execute();
}
.execute();
}

public String loadJSONFromAsset(String filename)
Expand All @@ -190,14 +212,13 @@ public String loadJSONFromAsset(String filename)

try {
String url = this.resolveUrl(null, filename);
Log.d(LCAT, "JSON Path: " + url);

InputStream inStream = TiFileFactory.createTitaniumFile(new String[] { url }, false).getInputStream();
byte[] buffer = new byte[inStream.available()];
inStream.read(buffer);
inStream.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
Log.e(LCAT, "Error opening file: " + ex.getMessage());
return "";
}
return json;
Expand Down