Skip to content

Commit 11bf106

Browse files
authored
Merge pull request #29 from m1ga/bugifx
Android: readme, bugfix for configure()
2 parents 24450fe + 8aa0404 commit 11bf106

File tree

3 files changed

+119
-85
lines changed

3 files changed

+119
-85
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ Use the native Firebase SDK in Axway Titanium. This repository is part of the [T
1818

1919
Configure Firebase without additional parameters.
2020

21+
<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.
22+
2123
##### `configure(parameters)`
2224

23-
Configure Firebase without configuration parameters.
25+
Configure Firebase with configuration parameters:
26+
27+
| Name | Type | Component | Platform |
28+
| - | - | - | - |
29+
| `file` | String | | *
30+
31+
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.
32+
33+
Or you can configure Firebase without a file by passing these parameters:
2434

2535
| Name | Type | Component | Platform |
2636
| - | - | - | - |
27-
| `file`* | String | | *
2837
| `googleAppID` | String | | *
2938
| `GCMSenderID` | String | Cloud Messaging | *
3039
| `APIKey` | String | Auth | *
@@ -38,16 +47,15 @@ Configure Firebase without configuration parameters.
3847
| `deepLinkURLScheme` | String | | iOS
3948
| `applicationID` | String | Analytics | Android
4049

41-
\* 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.
4250

4351
##### `deleteInstanceId(callback)`
4452

4553
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.
4654

4755
The callback receives an object containing this fields:
4856

49-
| Key | Type |Description |
50-
| - | - | - |
57+
| Key | Type | Description | Platform |
58+
| - | - | - | - |
5159
| `success` | Boolean | `true` if the deletion succeeded | *
5260
| `error` | String | The localized error message | *
5361

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

5866
The callback receives an object containing this fields:
5967

60-
| Key | Type | Description |
61-
| - | - | - |
68+
| Key | Type | Description | Platform |
69+
| - | - | - | - |
6270
| `success` | Boolean | `true` if the deletion succeeded | *
6371
| `error` | String | The localized error message | *
6472

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

7179
// Configure your Firebase API's (only required once for all)
80+
81+
FirebaseCore.configure(); // default google-services.json/GoogleService-Info.plist will be used
82+
83+
// alternative way:
84+
7285
FirebaseCore.configure({
7386
APIKey: "AIYasZBtfJh..........",
7487
projectID: "test-12345",

android/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 2.3.0
5+
version: 2.3.1
66
apiversion: 4
77
architectures: arm64-v8a armeabi-v7a x86
88
description: titanium-firebase-core

android/src/firebase/core/TitaniumFirebaseCoreModule.java

Lines changed: 98 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.InputStream;
3030
import java.lang.Void;
3131
import java.util.HashMap;
32+
import java.util.List;
3233

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

4647
@Kroll.method
47-
public void configure(@Kroll.argument(optional = true) KrollDict param)
48+
public boolean configure(@Kroll.argument(optional = true) KrollDict param)
4849
{
49-
if (param != null) {
50-
String apiKey = "";
51-
String databaseURL = "";
52-
String projectID = "";
53-
String storageBucket = "";
54-
String applicationID = "";
55-
String GCMSenderID = "";
56-
FirebaseOptions.Builder options = new FirebaseOptions.Builder();
57-
58-
if (param.containsKey("file")) {
59-
// open file and parse it
60-
try {
61-
JSONObject json = new JSONObject(loadJSONFromAsset(param.getString("file")));
62-
JSONObject projectInfo = json.getJSONObject("project_info");
63-
String packageName = TiApplication.getAppCurrentActivity().getPackageName();
50+
String filename = null;
6451

65-
if (projectInfo.has("storage_bucket")) {
66-
storageBucket = projectInfo.getString("storage_bucket");
67-
}
68-
if (projectInfo.has("firebase_url")) {
69-
databaseURL = projectInfo.getString("firebase_url");
70-
}
71-
if (projectInfo.has("project_number")) {
72-
GCMSenderID = projectInfo.getString("project_number");
73-
}
74-
if (projectInfo.has("project_id")) {
75-
projectID = projectInfo.getString("project_id");
76-
}
77-
if (json.has("client")) {
78-
JSONArray clients = json.getJSONArray("client");
79-
for (int i = 0, len = clients.length(); i < len; i++) {
80-
JSONObject client = clients.getJSONObject(i);
81-
JSONObject clientInfo = client.getJSONObject("client_info");
82-
String pName = clientInfo.getJSONObject("android_client_info").getString("package_name");
83-
if (pName.equals(packageName)) {
84-
applicationID = client.getJSONObject("client_info").getString("mobilesdk_app_id");
85-
apiKey = client.getJSONArray("api_key").getJSONObject(0).getString("current_key");
86-
}
87-
}
88-
}
89-
} catch (JSONException e) {
90-
Log.e(LCAT, "Error parsing file");
91-
}
92-
} else {
93-
// use parameters
94-
if (param.containsKey("APIKey")) {
95-
apiKey = param.getString("APIKey");
96-
}
97-
if (param.containsKey("databaseURL")) {
98-
databaseURL = param.getString("databaseURL");
52+
if (param == null) {
53+
filename = "google-services.json";
54+
} else if (param.containsKey("file")) {
55+
filename = param.getString("file");
56+
}
57+
String apiKey = "";
58+
String databaseURL = "";
59+
String projectID = "";
60+
String storageBucket = "";
61+
String applicationID = "";
62+
String GCMSenderID = "";
63+
FirebaseOptions.Builder options = new FirebaseOptions.Builder();
64+
65+
if (filename != null) {
66+
// open file and parse it
67+
try {
68+
JSONObject json = new JSONObject(loadJSONFromAsset(filename));
69+
JSONObject projectInfo = json.getJSONObject("project_info");
70+
String packageName = TiApplication.getAppCurrentActivity().getPackageName();
71+
72+
if (projectInfo.has("storage_bucket")) {
73+
storageBucket = projectInfo.getString("storage_bucket");
9974
}
100-
if (param.containsKey("projectID")) {
101-
projectID = param.getString("projectID");
75+
if (projectInfo.has("firebase_url")) {
76+
databaseURL = projectInfo.getString("firebase_url");
10277
}
103-
if (param.containsKey("storageBucket")) {
104-
storageBucket = param.getString("storageBucket");
78+
if (projectInfo.has("project_number")) {
79+
GCMSenderID = projectInfo.getString("project_number");
10580
}
106-
if (param.containsKey("applicationID")) {
107-
applicationID = param.getString("applicationID");
81+
if (projectInfo.has("project_id")) {
82+
projectID = projectInfo.getString("project_id");
10883
}
109-
if (param.containsKey("GCMSenderID")) {
110-
GCMSenderID = param.getString("GCMSenderID");
84+
if (json.has("client")) {
85+
JSONArray clients = json.getJSONArray("client");
86+
for (int i = 0, len = clients.length(); i < len; i++) {
87+
JSONObject client = clients.getJSONObject(i);
88+
JSONObject clientInfo = client.getJSONObject("client_info");
89+
String pName = clientInfo.getJSONObject("android_client_info").getString("package_name");
90+
if (pName.equals(packageName)) {
91+
applicationID = client.getJSONObject("client_info").getString("mobilesdk_app_id");
92+
apiKey = client.getJSONArray("api_key").getJSONObject(0).getString("current_key");
93+
}
94+
}
11195
}
96+
} catch (JSONException e) {
97+
Log.e(LCAT, "Error parsing file");
98+
}
99+
} else {
100+
// use parameters
101+
if (param.containsKey("APIKey")) {
102+
apiKey = param.getString("APIKey");
112103
}
104+
if (param.containsKey("databaseURL")) {
105+
databaseURL = param.getString("databaseURL");
106+
}
107+
if (param.containsKey("projectID")) {
108+
projectID = param.getString("projectID");
109+
}
110+
if (param.containsKey("storageBucket")) {
111+
storageBucket = param.getString("storageBucket");
112+
}
113+
if (param.containsKey("applicationID")) {
114+
applicationID = param.getString("applicationID");
115+
}
116+
if (param.containsKey("GCMSenderID")) {
117+
GCMSenderID = param.getString("GCMSenderID");
118+
}
119+
}
113120

114-
options.setApiKey(apiKey);
115-
options.setDatabaseUrl(databaseURL);
116-
options.setProjectId(projectID);
117-
options.setStorageBucket(storageBucket);
118-
options.setApplicationId(applicationID);
119-
options.setGcmSenderId(GCMSenderID);
121+
options.setApiKey(apiKey);
122+
options.setDatabaseUrl(databaseURL);
123+
options.setProjectId(projectID);
124+
options.setStorageBucket(storageBucket);
125+
options.setApplicationId(applicationID);
126+
options.setGcmSenderId(GCMSenderID);
127+
128+
// check for existing firebaseApp
129+
boolean hasBeenInitialized = false;
130+
List<FirebaseApp> fbsLcl = FirebaseApp.getApps(getActivity().getApplicationContext());
131+
for (FirebaseApp app : fbsLcl) {
132+
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) {
133+
hasBeenInitialized = true;
134+
}
135+
}
120136

137+
if (!hasBeenInitialized) {
121138
try {
122139
FirebaseApp.initializeApp(getActivity().getApplicationContext(), options.build());
140+
return true;
123141
} catch (IllegalStateException e) {
124142
Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time.");
143+
return false;
125144
}
126145
} else {
127-
try {
128-
FirebaseApp.initializeApp(getActivity().getApplicationContext());
129-
} catch (IllegalStateException e) {
130-
Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time.");
131-
}
146+
Log.d(LCAT, "FirebaseApp is alraedy initialized.");
147+
return false;
132148
}
133149
}
134150

135151
@Kroll.method
136152
public void deleteInstanceId(final KrollFunction callback)
137153
{
138154
new AsyncTask<Void, Void, IOException>() {
139-
protected IOException doInBackground(Void ... v) {
155+
protected IOException doInBackground(Void... v)
156+
{
140157
try {
141158
FirebaseInstanceId.getInstance().deleteInstanceId();
142159
return null;
@@ -145,7 +162,8 @@ protected IOException doInBackground(Void ... v) {
145162
return e;
146163
}
147164
}
148-
protected void onPostExecute(IOException error) {
165+
protected void onPostExecute(IOException error)
166+
{
149167
if (callback != null) {
150168
HashMap args = new HashMap<>();
151169
args.put("success", error == null);
@@ -155,14 +173,16 @@ protected void onPostExecute(IOException error) {
155173
callback.call(getKrollObject(), args);
156174
}
157175
}
158-
}.execute();
176+
}
177+
.execute();
159178
}
160179

161180
@Kroll.method
162181
public void deleteToken(final String authorizedEntity, final String scope, final KrollFunction callback)
163182
{
164183
new AsyncTask<Void, Void, IOException>() {
165-
protected IOException doInBackground(Void ... v) {
184+
protected IOException doInBackground(Void... v)
185+
{
166186
try {
167187
FirebaseInstanceId.getInstance().deleteToken(authorizedEntity, scope);
168188
return null;
@@ -171,7 +191,8 @@ protected IOException doInBackground(Void ... v) {
171191
return e;
172192
}
173193
}
174-
protected void onPostExecute(IOException error) {
194+
protected void onPostExecute(IOException error)
195+
{
175196
if (callback != null) {
176197
HashMap args = new HashMap<>();
177198
args.put("success", error == null);
@@ -181,7 +202,8 @@ protected void onPostExecute(IOException error) {
181202
callback.call(getKrollObject(), args);
182203
}
183204
}
184-
}.execute();
205+
}
206+
.execute();
185207
}
186208

187209
public String loadJSONFromAsset(String filename)
@@ -190,14 +212,13 @@ public String loadJSONFromAsset(String filename)
190212

191213
try {
192214
String url = this.resolveUrl(null, filename);
193-
Log.d(LCAT, "JSON Path: " + url);
194-
195215
InputStream inStream = TiFileFactory.createTitaniumFile(new String[] { url }, false).getInputStream();
196216
byte[] buffer = new byte[inStream.available()];
197217
inStream.read(buffer);
198218
inStream.close();
199219
json = new String(buffer, "UTF-8");
200220
} catch (IOException ex) {
221+
Log.e(LCAT, "Error opening file: " + ex.getMessage());
201222
return "";
202223
}
203224
return json;

0 commit comments

Comments
 (0)