-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP #8 - Converting JSON data to Java objects
- Added Gson dependency (library for manipulating Json) - Moved json files to a dedicated directory - Created main for simple test - Added Java classes to represent Json data
- Loading branch information
1 parent
1de7f8b
commit 3c0b577
Showing
6 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{ | ||
"sourceName": "Activity1", | ||
"declaredPkg": "br.ufpe.cin.androidintenttests", | ||
"intents": [{ | ||
"declaredMethod": "func1", | ||
"target" : "startActivity", | ||
"identifier" : "toActivity2", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW", | ||
"data": "Uri.parse(\"custom://base#specific\")" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func1", | ||
"target" : "startActivity", | ||
"identifier" : "i", | ||
"properties": { | ||
"action": "Intent.ACTION_CALL", | ||
"data": "Uri.parse(\"tel:123456789\")" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target" : "startActivity", | ||
"identifier": "i", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW", | ||
"data": "Uri.parse(\"https://www.google.com\")" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target" : "startActivity", | ||
"identifier": "i2", | ||
"properties": { | ||
"component": { | ||
"context": "br.ufpe.cin.androidintenttests", | ||
"class": "Activity1" | ||
}, | ||
"data": "Uri.parse(\"https://www.google.com\")" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target" : "startActivity", | ||
"identifier": "i3", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW", | ||
"data": "Uri.parse(\"https://www.google.com\")" | ||
} | ||
}, | ||
{ | ||
"target" : "startActivity", | ||
"declaredMethod": "func2", | ||
"identifier": "i4", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW", | ||
"data": "Uri.parse(\"https://www.google.com\")", | ||
"type": "text/plain" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target" : "startActivity", | ||
"identifier": "i5", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW", | ||
"data": "Uri.parse(\"https://www.google.com\")", | ||
"type": "text/plain" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target" : "startActivity", | ||
"identifier": "i6", | ||
"properties": { | ||
"component": { | ||
"context": "br.ufpe.cin.androidintenttests", | ||
"class": "Activity1" | ||
}, | ||
"data": "Uri.parse(\"https://www.google.com\")", | ||
"type": "text/plain" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
{ | ||
"sourceName": "Activity2", | ||
"declaredPkg": "br.ufpe.cin.androidintenttests", | ||
"intents": [{ | ||
"declaredMethod": "func1", | ||
"target" : "startActivity", | ||
"identifier" : "i", | ||
"properties": { | ||
"component": { | ||
"context": "this", | ||
"class": "Activity1.class" | ||
}, | ||
"data": "Uri.parse(\"custom://base#specific\")" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func1", | ||
"target": "startActivity", | ||
"identifier": "i2", | ||
"properties": { | ||
"component": { | ||
"context": "this", | ||
"class": "Activity1.class" | ||
}, | ||
"data": "Uri.parse(\"custom://base#specific\")", | ||
"extra": { | ||
"key_bool": false, | ||
"key_string": "somevalue", | ||
"key_int": 10, | ||
"key_double": 12.5 | ||
} | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target": "startActivity", | ||
"identifier": "i1", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target": "startActivity", | ||
"identifier": "i2", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW", | ||
"data": "Uri.parse(\"http://www.google.com\")" | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target": "startActivity", | ||
"identifier": "i3", | ||
"properties": { | ||
"component": { | ||
"context": "this", | ||
"class": "Activity1.class" | ||
} | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func2", | ||
"target": "startActivity", | ||
"identifier": "i4", | ||
"properties": { | ||
"action": "Intent.ACTION_VIEW", | ||
"data": "Uri.parse(\"http://www.google.com\")", | ||
"component": { | ||
"context": "this", | ||
"class": "Activity1.class" | ||
} | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func3", | ||
"target": "startActivity", | ||
"identifier": "i", | ||
"properties": { | ||
"package": "br.ufpe.cin.androidintenttests", | ||
"component": { | ||
"context": "this", | ||
"class": "Activity" | ||
} | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func3", | ||
"target": "startActivity", | ||
"identifier": "i2", | ||
"properties": { | ||
"data": "Uri.parse(\"https://www.google.com\")", | ||
"component": { | ||
"context": "br.ufpe.cin.androidintenttests", | ||
"class": "Activity1" | ||
} | ||
} | ||
}, | ||
{ | ||
"declaredMethod": "func3", | ||
"target": "startActivity", | ||
"identifier": "i3", | ||
"properties": { | ||
"component": { | ||
"context": "this", | ||
"class": "Activity1" | ||
}, | ||
"data": "Uri.parse(\"https://www.google.com\")" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package subjects.mock; | ||
|
||
/* | ||
* FIXME: Not finished yet | ||
*/ | ||
public class IntentDataJson { | ||
|
||
/* | ||
* FIXME: Not finished yet | ||
*/ | ||
class IntentProperties { | ||
private String action; | ||
private String data; | ||
|
||
@Override | ||
public String toString() { | ||
return action; | ||
} | ||
|
||
} | ||
|
||
private String declaredMethod; | ||
private String target; | ||
private String identifier; | ||
private IntentProperties properties; | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder("{\n"); | ||
|
||
if (this.declaredMethod != null) | ||
sb.append("declaredMethod: ").append(this.declaredMethod).append("\n"); | ||
if (this.target != null) | ||
sb.append("target: ").append(this.target).append("\n"); | ||
if (this.identifier != null) | ||
sb.append("identifier: ").append(this.identifier).append("\n"); | ||
if (this.properties != null) | ||
sb.append("properties: {").append(this.properties).append("}\n"); | ||
|
||
sb.append("}"); | ||
|
||
return sb.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package subjects.mock; | ||
|
||
import java.util.List; | ||
|
||
public class IntentResultJson { | ||
|
||
/* | ||
* Field names matches JSON field data | ||
*/ | ||
private String sourceName; | ||
private String declaredPkg; | ||
private List<IntentDataJson> intents; | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
sb.append("sourceName: ").append(this.sourceName).append("\n"); | ||
sb.append("declaredPkg: ").append(this.declaredPkg).append("\n"); | ||
sb.append("intents: ").append(this.intents).append("\n"); | ||
|
||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package subjects.mock; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws IOException { | ||
String jsonString1 = extractStringFrom("mock-data/intent_tests/Activity1-Intents.json"); | ||
String jsonString2 = extractStringFrom("mock-data/intent_tests/Activity2-Intents.json"); | ||
|
||
Gson gson = new GsonBuilder().create(); | ||
|
||
// Magic is done here with Reflection. GSON automatically matches JSON | ||
// attributes/values with the target class by names. | ||
IntentResultJson data1 = gson.fromJson(jsonString1, IntentResultJson.class); | ||
IntentResultJson data2 = gson.fromJson(jsonString2, IntentResultJson.class); | ||
|
||
System.out.println(data1); | ||
System.out.println(data2); | ||
} | ||
|
||
/** | ||
* Reads a file and returns a whole string from the read content | ||
* | ||
* @param filePath | ||
* Path to the file to be read | ||
* @return The content of filePath as String | ||
* @throws FileNotFoundException | ||
* @throws IOException | ||
*/ | ||
private static String extractStringFrom(String filePath) throws FileNotFoundException, IOException { | ||
BufferedReader br = new BufferedReader(new FileReader(filePath)); | ||
StringBuilder sb = new StringBuilder(); | ||
while (br.ready()) { | ||
sb.append(br.readLine().trim()); | ||
} | ||
br.close(); | ||
|
||
return sb.toString(); | ||
} | ||
} |