|
25 | 25 | import java.io.InputStream;
|
26 | 26 |
|
27 | 27 | @Kroll.module(name = "TitaniumFirebaseCore", id = "firebase.core")
|
28 |
| -public class TitaniumFirebaseCoreModule extends KrollModule { |
29 |
| - private static final String LCAT = "TitaniumFirebaseCoreModule"; |
30 |
| - private static final boolean DBG = TiConfig.LOGD; |
| 28 | +public class TitaniumFirebaseCoreModule extends KrollModule |
| 29 | +{ |
| 30 | + private static final String LCAT = "TitaniumFirebaseCoreModule"; |
| 31 | + private static final boolean DBG = TiConfig.LOGD; |
31 | 32 |
|
32 |
| - public TitaniumFirebaseCoreModule() { super(); } |
| 33 | + public TitaniumFirebaseCoreModule() |
| 34 | + { |
| 35 | + super(); |
| 36 | + } |
33 | 37 |
|
34 |
| - // Public APIs |
| 38 | + // Public APIs |
35 | 39 |
|
36 |
| - @Kroll.method |
37 |
| - public void configure(@Kroll.argument(optional = true) KrollDict param) { |
38 |
| - if (param != null) { |
39 |
| - String apiKey = ""; |
40 |
| - String databaseURL = ""; |
41 |
| - String projectID = ""; |
42 |
| - String storageBucket = ""; |
43 |
| - String applicationID = ""; |
44 |
| - String GCMSenderID = ""; |
45 |
| - FirebaseOptions.Builder options = new FirebaseOptions.Builder(); |
| 40 | + @Kroll.method |
| 41 | + public void configure(@Kroll.argument(optional = true) KrollDict param) |
| 42 | + { |
| 43 | + if (param != null) { |
| 44 | + String apiKey = ""; |
| 45 | + String databaseURL = ""; |
| 46 | + String projectID = ""; |
| 47 | + String storageBucket = ""; |
| 48 | + String applicationID = ""; |
| 49 | + String GCMSenderID = ""; |
| 50 | + FirebaseOptions.Builder options = new FirebaseOptions.Builder(); |
46 | 51 |
|
47 |
| - if (param.containsKey("file")) { |
48 |
| - // open file and parse it |
49 |
| - try { |
50 |
| - JSONObject json = |
51 |
| - new JSONObject(loadJSONFromAsset(param.getString("file"))); |
52 |
| - JSONObject projectInfo = json.getJSONObject("project_info"); |
53 |
| - String packageName = |
54 |
| - TiApplication.getAppCurrentActivity().getPackageName(); |
| 52 | + if (param.containsKey("file")) { |
| 53 | + // open file and parse it |
| 54 | + try { |
| 55 | + JSONObject json = new JSONObject(loadJSONFromAsset(param.getString("file"))); |
| 56 | + JSONObject projectInfo = json.getJSONObject("project_info"); |
| 57 | + String packageName = TiApplication.getAppCurrentActivity().getPackageName(); |
55 | 58 |
|
56 |
| - if (projectInfo.has("storage_bucket")) { |
57 |
| - storageBucket = projectInfo.getString("storage_bucket"); |
58 |
| - } |
59 |
| - if (projectInfo.has("firebase_url")) { |
60 |
| - databaseURL = projectInfo.getString("firebase_url"); |
61 |
| - } |
62 |
| - if (projectInfo.has("project_number")) { |
63 |
| - GCMSenderID = projectInfo.getString("project_number"); |
64 |
| - } |
65 |
| - if (projectInfo.has("project_id")) { |
66 |
| - projectID = projectInfo.getString("project_id"); |
67 |
| - } |
68 |
| - if (json.has("client")) { |
69 |
| - JSONArray clients = json.getJSONArray("client"); |
70 |
| - for (int i = 0, len = clients.length(); i < len; i++) { |
71 |
| - JSONObject client = clients.getJSONObject(i); |
72 |
| - JSONObject clientInfo = client.getJSONObject("client_info"); |
73 |
| - String pName = clientInfo.getJSONObject("android_client_info") |
74 |
| - .getString("package_name"); |
75 |
| - if (pName.equals(packageName)) { |
76 |
| - applicationID = client.getJSONObject("client_info") |
77 |
| - .getString("mobilesdk_app_id"); |
78 |
| - apiKey = |
79 |
| - client.getJSONArray("api_key").getJSONObject(0).getString( |
80 |
| - "current_key"); |
81 |
| - } |
82 |
| - } |
83 |
| - } |
84 |
| - } catch (JSONException e) { |
85 |
| - Log.e(LCAT, "Error parsing file"); |
86 |
| - } |
87 |
| - } else { |
88 |
| - // use parameters |
89 |
| - if (param.containsKey("APIKey")) { |
90 |
| - apiKey = param.getString("APIKey"); |
91 |
| - } |
92 |
| - if (param.containsKey("databaseURL")) { |
93 |
| - databaseURL = param.getString("databaseURL"); |
94 |
| - } |
95 |
| - if (param.containsKey("projectID")) { |
96 |
| - projectID = param.getString("projectID"); |
97 |
| - } |
98 |
| - if (param.containsKey("storageBucket")) { |
99 |
| - storageBucket = param.getString("storageBucket"); |
100 |
| - } |
101 |
| - if (param.containsKey("applicationID")) { |
102 |
| - applicationID = param.getString("applicationID"); |
103 |
| - } |
104 |
| - if (param.containsKey("GCMSenderID")) { |
105 |
| - GCMSenderID = param.getString("GCMSenderID"); |
106 |
| - } |
107 |
| - } |
| 59 | + if (projectInfo.has("storage_bucket")) { |
| 60 | + storageBucket = projectInfo.getString("storage_bucket"); |
| 61 | + } |
| 62 | + if (projectInfo.has("firebase_url")) { |
| 63 | + databaseURL = projectInfo.getString("firebase_url"); |
| 64 | + } |
| 65 | + if (projectInfo.has("project_number")) { |
| 66 | + GCMSenderID = projectInfo.getString("project_number"); |
| 67 | + } |
| 68 | + if (projectInfo.has("project_id")) { |
| 69 | + projectID = projectInfo.getString("project_id"); |
| 70 | + } |
| 71 | + if (json.has("client")) { |
| 72 | + JSONArray clients = json.getJSONArray("client"); |
| 73 | + for (int i = 0, len = clients.length(); i < len; i++) { |
| 74 | + JSONObject client = clients.getJSONObject(i); |
| 75 | + JSONObject clientInfo = client.getJSONObject("client_info"); |
| 76 | + String pName = clientInfo.getJSONObject("android_client_info").getString("package_name"); |
| 77 | + if (pName.equals(packageName)) { |
| 78 | + applicationID = client.getJSONObject("client_info").getString("mobilesdk_app_id"); |
| 79 | + apiKey = client.getJSONArray("api_key").getJSONObject(0).getString("current_key"); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } catch (JSONException e) { |
| 84 | + Log.e(LCAT, "Error parsing file"); |
| 85 | + } |
| 86 | + } else { |
| 87 | + // use parameters |
| 88 | + if (param.containsKey("APIKey")) { |
| 89 | + apiKey = param.getString("APIKey"); |
| 90 | + } |
| 91 | + if (param.containsKey("databaseURL")) { |
| 92 | + databaseURL = param.getString("databaseURL"); |
| 93 | + } |
| 94 | + if (param.containsKey("projectID")) { |
| 95 | + projectID = param.getString("projectID"); |
| 96 | + } |
| 97 | + if (param.containsKey("storageBucket")) { |
| 98 | + storageBucket = param.getString("storageBucket"); |
| 99 | + } |
| 100 | + if (param.containsKey("applicationID")) { |
| 101 | + applicationID = param.getString("applicationID"); |
| 102 | + } |
| 103 | + if (param.containsKey("GCMSenderID")) { |
| 104 | + GCMSenderID = param.getString("GCMSenderID"); |
| 105 | + } |
| 106 | + } |
108 | 107 |
|
109 |
| - options.setApiKey(apiKey); |
110 |
| - options.setDatabaseUrl(databaseURL); |
111 |
| - options.setProjectId(projectID); |
112 |
| - options.setStorageBucket(storageBucket); |
113 |
| - options.setApplicationId(applicationID); |
114 |
| - options.setGcmSenderId(GCMSenderID); |
| 108 | + options.setApiKey(apiKey); |
| 109 | + options.setDatabaseUrl(databaseURL); |
| 110 | + options.setProjectId(projectID); |
| 111 | + options.setStorageBucket(storageBucket); |
| 112 | + options.setApplicationId(applicationID); |
| 113 | + options.setGcmSenderId(GCMSenderID); |
115 | 114 |
|
116 |
| - try { |
117 |
| - FirebaseApp.initializeApp(getActivity().getApplicationContext(), |
118 |
| - options.build()); |
119 |
| - } catch (IllegalStateException e) { |
120 |
| - Log.w( |
121 |
| - LCAT, |
122 |
| - "There was a problem initializing FirebaseApp or it was initialized a second time."); |
123 |
| - } |
124 |
| - } else { |
125 |
| - try { |
126 |
| - FirebaseApp.initializeApp(getActivity().getApplicationContext()); |
127 |
| - } catch (IllegalStateException e) { |
128 |
| - Log.w( |
129 |
| - LCAT, |
130 |
| - "There was a problem initializing FirebaseApp or it was initialized a second time."); |
131 |
| - } |
132 |
| - } |
133 |
| - } |
| 115 | + try { |
| 116 | + FirebaseApp.initializeApp(getActivity().getApplicationContext(), options.build()); |
| 117 | + } catch (IllegalStateException e) { |
| 118 | + Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time."); |
| 119 | + } |
| 120 | + } else { |
| 121 | + try { |
| 122 | + FirebaseApp.initializeApp(getActivity().getApplicationContext()); |
| 123 | + } catch (IllegalStateException e) { |
| 124 | + Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time."); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
134 | 128 |
|
135 |
| - public String loadJSONFromAsset(String filename) { |
136 |
| - String json = null; |
| 129 | + public String loadJSONFromAsset(String filename) |
| 130 | + { |
| 131 | + String json = null; |
137 | 132 |
|
138 |
| - try { |
139 |
| - String url = this.resolveUrl(null, filename); |
140 |
| - Log.d(LCAT, "JSON Path: " + url); |
| 133 | + try { |
| 134 | + String url = this.resolveUrl(null, filename); |
| 135 | + Log.d(LCAT, "JSON Path: " + url); |
141 | 136 |
|
142 |
| - InputStream inStream = |
143 |
| - TiFileFactory.createTitaniumFile(new String[] {url}, false) |
144 |
| - .getInputStream(); |
145 |
| - byte[] buffer = new byte[inStream.available()]; |
146 |
| - inStream.read(buffer); |
147 |
| - inStream.close(); |
148 |
| - json = new String(buffer, "UTF-8"); |
149 |
| - } catch (IOException ex) { |
150 |
| - Log.e(LCAT, "Error reading file"); |
151 |
| - return ""; |
152 |
| - } |
153 |
| - return json; |
154 |
| - } |
| 137 | + InputStream inStream = TiFileFactory.createTitaniumFile(new String[] { url }, false).getInputStream(); |
| 138 | + byte[] buffer = new byte[inStream.available()]; |
| 139 | + inStream.read(buffer); |
| 140 | + inStream.close(); |
| 141 | + json = new String(buffer, "UTF-8"); |
| 142 | + } catch (IOException ex) { |
| 143 | + Log.e(LCAT, "Error reading file"); |
| 144 | + return ""; |
| 145 | + } |
| 146 | + return json; |
| 147 | + } |
155 | 148 | }
|
0 commit comments