11package net .codebuff .intentio .parser ;
22
3+ import android .content .ContentResolver ;
34import android .content .Context ;
45import android .net .Uri ;
56import android .util .Log ;
7+ import android .webkit .MimeTypeMap ;
68
79import net .codebuff .intentio .helpers .Constants ;
810import net .codebuff .intentio .helpers .Utilities ;
1517
1618import java .io .File ;
1719import java .io .FileInputStream ;
20+ import java .io .FileNotFoundException ;
1821import java .io .IOException ;
1922import java .io .InputStream ;
2023import java .util .Calendar ;
@@ -34,6 +37,7 @@ public class Parser {
3437 private File excel ;
3538 PrefsManager prefs ;
3639 Uri uri ;
40+ ContentResolver cR ;
3741
3842 public Parser (Context context ) {
3943 // will be used when we move to restricted file storage.
@@ -51,24 +55,35 @@ public Parser() {
5155 }
5256
5357 // following methods shows a giant middle finger, everytime someone does careless implementation,be careful
54- public void open_file () {
55-
56- // the file will be hardcoded in some way later
57- //File dir = Environment.getExternalStorageDirectory(); // dangerous implementation but serves practical purpose
58-
59- //File dir = context.getFilesDir(); // maybe we can switch to this later.
60- //Log.i("external storage diretory", dir.getPath());
61- //excel = new File(dir, "test.xls");
62-
63- if (uri .getLastPathSegment ().contains (".xlsx" )) {
64- excel = null ;
65- } else if (uri .getLastPathSegment ().contains (".xls" )) {
66- excel = new File (uri .getPath ());
67- } else {
68- excel = null ;
58+ public InputStream getInputStream () {
59+ cR = context .getContentResolver ();
60+ String uriStr = uri .toString ();
61+ //Log.i("uri", uriStr );
62+ if (uriStr .startsWith ("file://" )) {
63+ // contentResolver can't get type from file links
64+ // falling back to check for xls in string
65+ if (uriStr .endsWith (".xls" )) {
66+ try {
67+ return cR .openInputStream (uri );
68+ } catch (FileNotFoundException e ) {
69+ e .printStackTrace ();
70+ return null ;
71+ }
72+ }
73+ } else if (uriStr .startsWith ("content://" )) {
74+ // contentResolver can get the type
75+ MimeTypeMap mime = MimeTypeMap .getSingleton ();
76+ String type = mime .getExtensionFromMimeType (cR .getType (uri ));
77+ if (type .equals ("xls" )) {
78+ try {
79+ return cR .openInputStream (uri );
80+ } catch (FileNotFoundException e ) {
81+ e .printStackTrace ();
82+ return null ;
83+ }
84+ }
6985 }
70-
71-
86+ return null ;
7287 }
7388
7489 public String parse_excel () throws IOException {
@@ -88,22 +103,23 @@ public String parse_excel() throws IOException {
88103
89104 String content = "" ;
90105
91- Calendar calendar = Calendar .getInstance ();
92- // Log.e("day", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()));
93- // find and open the xl file.
94- open_file ();
106+ InputStream inputStream = getInputStream ();
95107
96- if ((excel == null ) || (!excel .isFile ())) {
97- // Toast.makeText(context, "File not found or incorrect file provided", Toast.LENGTH_LONG).show();
108+ if (inputStream == null ) {
98109 return "file not found" ;
99- } else {
100- prefs = new PrefsManager (context );
101110 }
102111
112+ //Calendar calendar = Calendar.getInstance();
113+ // Log.e("day", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()));
103114
104- InputStream inputStream = new FileInputStream (excel );
115+ prefs = new PrefsManager (context );
116+ try { // although we check at inputstream, a xlsx may still creep in.
117+ wb = new HSSFWorkbook (inputStream );
118+ } catch (Exception e ) {
119+ e .printStackTrace ();
120+ return "file not found" ;
121+ }
105122
106- wb = new HSSFWorkbook (inputStream );
107123
108124 if (wb != null ) {
109125 Log .e ("excel" , "starting dump" );
@@ -144,7 +160,7 @@ public String parse_excel() throws IOException {
144160 if (cell_data .toLowerCase ().contains ("sun" )) {
145161 last_day_row_found = true ;
146162 }
147- if (day_column_found && c == day_column_number ){
163+ if (day_column_found && c == day_column_number ) {
148164 day = Utilities .get_day_name (cell_data );
149165 }
150166 break ;
@@ -164,7 +180,7 @@ public String parse_excel() throws IOException {
164180 if (cell_data .toLowerCase ().contains ("sun" )) {
165181 last_day_row_found = true ;
166182 }
167- if (day_column_found && c == day_column_number ){
183+ if (day_column_found && c == day_column_number ) {
168184 day = Utilities .get_day_name (cell_data );
169185 }
170186 break ;
@@ -185,7 +201,7 @@ public String parse_excel() throws IOException {
185201 if (cell_data .toLowerCase ().contains ("sun" )) {
186202 last_day_row_found = true ;
187203 }
188- if (day_column_found && c == day_column_number ){
204+ if (day_column_found && c == day_column_number ) {
189205 day = Utilities .get_day_name (cell_data );
190206 }
191207 break ;
@@ -217,17 +233,17 @@ public String parse_excel() throws IOException {
217233
218234 if (schedule_found && !schedule_processed && slots_processed ) {
219235
220- // Log.e("row no",Integer.toString(row.getRowNum()));
221- if (cell_data == null || cell_data .trim () == "" ) {
222- cell_data = Constants .empty_slot ;
223- }
224- if (slots .containsKey (cell .getColumnIndex ()) && !day .equals ("invalid" )) {
225- prefs .save_schedule (day , slots .get (cell .getColumnIndex ()), cell_data .trim ());
226- }
236+ // Log.e("row no",Integer.toString(row.getRowNum()));
237+ if (cell_data == null || cell_data .trim () == "" ) {
238+ cell_data = Constants .empty_slot ;
239+ }
240+ if (slots .containsKey (cell .getColumnIndex ()) && !day .equals ("invalid" )) {
241+ prefs .save_schedule (day , slots .get (cell .getColumnIndex ()), cell_data .trim ());
242+ }
227243
228- if (last_day_row_found && (c == (cells - 1 ))) {
229- schedule_processed = true ;
230- }
244+ if (last_day_row_found && (c == (cells - 1 ))) {
245+ schedule_processed = true ;
246+ }
231247 }
232248 content = content + "CELL col=" + cell .getColumnIndex () + " VALUE="
233249 + value + "\n " ;
@@ -240,13 +256,14 @@ public String parse_excel() throws IOException {
240256 if (schedule_processed ) break ;
241257
242258 }
259+ } else {
260+ content = "file not found" ;
243261 }
244262 //Log.i("content", content);
245263 return content ;
246264 }
247265
248266
249-
250267}
251268
252269
0 commit comments