-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
181 lines (132 loc) · 6.31 KB
/
MainActivity.java
File metadata and controls
181 lines (132 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.example.sudarshanseshadri.bloombergjson;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.BarGraphSeries;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.PointsGraphSeries;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.Collectors;
public class MainActivity extends AppCompatActivity {
final String TAG = "TAG_MainActivity";
final int CHOOSE_FILE_REQUEST_CODE = 1;
Button chooseButton;
ImageView chooseImage;
JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chooseButton=findViewById(R.id.id_button_choose_file);
chooseImage=findViewById(R.id.id_imageView);
//both the image and the button, when clicked on, will pomt the user to select a json file from their file manager
chooseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//intent.setType("*/*"); //all files
intent.setType("application/json"); //allow user to select only json files--some other types may also be selectable though.
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), CHOOSE_FILE_REQUEST_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// If the device doesn't have a file viewer
Toast.makeText(MainActivity.this, "Please install a File Manager.", Toast.LENGTH_LONG).show();
}
}
});
chooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//intent.setType("*/*"); //all files
intent.setType("application/json"); //allow user to select only json files--some other types may also be selectable though.
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), CHOOSE_FILE_REQUEST_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// If the device doesn't have a file viewer
Toast.makeText(MainActivity.this, "Please install a File Manager.", Toast.LENGTH_LONG).show();
}
}
});
}
//to receive the JSON file that the user has selected and retrieve the text. If valid, go to the next activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch(requestCode)
{
case CHOOSE_FILE_REQUEST_CODE:
{
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String mimeType = getContentResolver().getType(uri);
//find the file's MIME type -- to find, again, if this really is a json file.
if (mimeType.equals("application/json"))
{
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
//scanner iterate over tokens in the stream, so the beggining token of \A is the only one it goes over.
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String JSONFileText="";
if (s.hasNext())
{
JSONFileText=s.next();
}
s.close();
checkForValidJSON(JSONFileText);
Intent i = new Intent(this, GraphSettingsActivity.class);
i.putExtra("JSONFile", JSONFileText);
startActivity(i);
} catch (FileNotFoundException e) {
e.printStackTrace();
//just in case
Toast.makeText(MainActivity.this,"Please choose a valid JSON array", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,"Please choose a valid JSON array", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this,"Please choose a valid JSON array", Toast.LENGTH_SHORT).show();
}
}
}
}
}
public void checkForValidJSON(String JSONFileText) throws JSONException
{
// Checking if it can be a valid JSON array.
jsonArray=new JSONArray(JSONFileText);
}
}