aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/Utils.java
blob: bb3c68267083eac13edea6fd4b63e8e3ae0b462d (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.helpers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import fil.libre.repwifiapp.Commons;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.Log;

public class Utils {

	private static final long MILLIS_IN_DAY = 86400000;
	

	public static final String APP_NAME = "RepWifi";
		
	private static Exception _lastException = null;
	
	public static Exception getLastException(){
		return _lastException;
	}
	
	public static void logError(String msg, Exception e){
		Log.e(APP_NAME,msg,e);
	}
	
	public static void logError(String msg){
		Log.e(APP_NAME,msg);
	}
	
	public static void logDebug(String msg){
		logDebug(msg,0);
	}
	
	public static void logDebug(String msg, int level){
		
		if (level < Commons.getLogPriority()){
			return;
		}
		
		Log.d(APP_NAME,msg);
	}
	

	public static boolean writeFile(String filePath, String text, boolean overwrite){
		
			
		FileWriter writer = null;
		boolean retval = false;
		try {
			
			writer = new FileWriter(filePath, (! overwrite));
			writer.write(text);
					
			retval = true;
			
		} catch (Exception e) {
			_lastException = e;
			retval = false;
		}
		finally{
			
			if (writer != null){
				try {
					writer.close();
				} catch (IOException e) {
					//suppress
				}
			}		
					
		}
		
		return retval;
		
	}
	
	public static boolean writeFileLines(String filePath, String[] lines, boolean overwrite){
		
		if (lines == null){
			return false;
		}
		
		FileWriter writer = null;
		boolean retval = false;
		try {
			
			writer = new FileWriter(filePath, (! overwrite));
			
			if (lines.length == 0){
				writer.write("");
			}
			
			for(String l : lines){
				writer.write(l + "\n");
			}
					
			retval = true;
			
		} catch (Exception e) {
			_lastException = e;
			retval = false;
		}
		finally{
			
			if (writer != null){
				try {
					writer.close();
				} catch (IOException e) {
					//suppress
				}
			}		
					
		}
		
		return retval;
		
	}

	public static String[] readFileLines(String filePath){
		
		if (filePath == null){
			return null;
		}
		
		File f = new File(filePath);
		if (! f.exists()){
			logError("File doesn't exist: " + filePath);
			return null;
		}
		
		FileReader fr = null;
        BufferedReader bufr = null;
		
        List<String> lines = new ArrayList<String>();
        String[] ret = null;
        
		try {
			
			fr = new FileReader(filePath);
			bufr = new BufferedReader(fr);
	        String line ="";
	        while((line = bufr.readLine()) != null){
	            lines.add(line);
	        }
	        
	        String[] ar = new String[lines.size()];
	        ret = lines.toArray(ar);
	        
		} catch (Exception e) {
			logError("Error while reading file " + filePath,e);
			ret = null;
		}
		finally{
			try {
				if (bufr != null){
					bufr.close();
				}
			} catch (IOException ex) {
				//suppress
			}
			try {
				if (fr != null){
					fr.close();
				}
			}catch(IOException exc){
				//suppress
			}
		}
        
		return ret;
		
	}

	public static long daysToMilliseconds(int days){		
		return (days * MILLIS_IN_DAY);		
	}
	
	public static long millisecondsToDays(long milliseconds){
		return (milliseconds / MILLIS_IN_DAY);
	}
	
}