aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/Commons.java
blob: 202e67f4176385186475b0fa828575538ed32579 (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
//
// 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;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import fil.libre.repwifiapp.helpers.Engine4p2;
import fil.libre.repwifiapp.helpers.Engine6p0;
import fil.libre.repwifiapp.helpers.IEngine;
import fil.libre.repwifiapp.helpers.NetworkManager;
import fil.libre.repwifiapp.helpers.Utils;



public abstract class Commons {

	private static Context currentContext;
	public Context getContext(){
		return currentContext;
	}
	
	//------------- Enviromnet Constants ----------------------------------------
	public static final int EXCOD_ROOT_DISABLED = 255;
	public static final int EXCOD_ROOT_DENIED = 1;
	public static final String v4p2 = "4.2";
	public static final String v6p0 = "6.0";
	public static final String SCAN_FILE_HDR = "bssid / frequency / signal level / flags / ssid";
	public static final String INTERFACE_NAME="wlan0";
	public static final String WORKDIR = "/data/misc/wifi";
	public static final String PID_FILE = WORKDIR + "/pidfile";
	public static final String SOCKET_DIR = WORKDIR + "/sockets/";
	public static final String SOFTAP_FILE = WORKDIR + "/softap.conf";
	public static final String P2P_CONF = WORKDIR + "/p2p_supplicant.conf";
	public static final String WPA_CONF = WORKDIR + "/wpa_supplicant.conf";
	public static final String ENTROPY_FILE = WORKDIR + "/entropy.bin";
	public static final String OVERLAY_FILE = "/system/etc/wifi/wpa_supplicant_overlay.conf";
	//------------------------------------------------------------------------------
			
	//------------- Shared Engines -----------------------
	public static IEngine connectionEngine = null;
	public static NetworkManager storage = null;
	//----------------------------------------------------
	
	
	//------------- Shared Resources ---------------------
	public static int colorThemeDark;
	public static int colorThemeLight;
	public static int colorBlack;
	
	public static int getLogPriority(){
		
		SharedPreferences sets = getSettings();
		return Integer.parseInt(sets.getString("debug_priority","3"));
		
	}
	
	public static SharedPreferences getSettings(){
		return PreferenceManager.getDefaultSharedPreferences(currentContext);
	}
	//----------------------------------------------------
	
	
	//------------- Activity Interaction -----------------
	public static final String EXTRA_APINFO = "ExAPInfo";
	public static final String EXTRA_APINFO_ARR = "ExAPInfoArr";
	public static final String EXTRA_CONSTATUS = "ExConSts";
	public static final String EXTRA_BOOLEAN = "ExBool";
	public static final String EXTRA_REQCODE = "ExReqCode";
	public static final String EXTRA_RESCAN = "ExRescan";
	public static final String EXTRA_DELETE = "ExDelete";
	
	public class RequestCode{
		public static final int NONE = 0;
		public static final int SELECT_CONN = 1;
		public static final int PASS_INPUT = 2;
		public static final int STATUS_SHOW = 3;
		public static final int STATUS_GET = 4;
		public static final int CONNECT = 5;
		public static final int NETWORKS_GET = 6;
		public static final int SELECT_DETAILS = 7;
		public static final int DETAILS_SHOW = 8;
		public static final int NETWORK_DELETE = 9;
	}
	//----------------------------------------------------
	

	//----------------- Application Files --------------------
	private static String APP_DATA_FOLDER;
	
	public static String getNetworkStorageFile(){
		if (APP_DATA_FOLDER == null){
			return null;
		}else{
			return APP_DATA_FOLDER + "/repwifi_storage.conf";
		}
	}
	public static String getScriptScan(){
		return APP_DATA_FOLDER + "/scan.sh";	
	}	
	public static String getScriptScanRes(){
		return APP_DATA_FOLDER + "/get_scan_results.sh";
	}
	public static String getScriptDhcpcd(){
		return APP_DATA_FOLDER + "/run_dhcpcd.sh";
	}
	public static String getScanFile(){
		return APP_DATA_FOLDER + "/scanres.txt";
	}
	public static String getStatusFile(){
		return APP_DATA_FOLDER + "/tmpStatus";
	}
	public static String getGwFile(){
		return APP_DATA_FOLDER + "/gw.txt";
	}
	public static String getTempOutFile(){
		return APP_DATA_FOLDER + "/tmpout.txt";
	}
	//--------------------------------------------------------
	
	
	//----------- Initialization methods ---------------------------
	public static boolean init(Context context){
		
		currentContext = context;
		
		try {
			

			colorThemeDark = currentContext.getResources().getColor(R.color.ThemeDark);
			colorThemeLight = currentContext.getResources().getColor(R.color.ThemeLight);
			colorBlack = currentContext.getResources().getColor(R.color.black);
			APP_DATA_FOLDER = currentContext.getExternalFilesDir(null).getAbsolutePath();
			
			initEngine();
			initNetworkStorage();
					
			
			return true;
			
		} catch (Exception e) {
			Utils.logError("Error initializing common resources.",e);
			return false;
		}
	}
	
	private static void initEngine() throws Exception{
		
		String vers = android.os.Build.VERSION.RELEASE;
		
		if (vers.startsWith(Commons.v4p2)){
			Commons.connectionEngine = new Engine4p2();
		}
		else if(vers.startsWith(Commons.v6p0)){
			Commons.connectionEngine = new Engine6p0();
		}
		else{
			throw new Exception("System version not recognized!");
		}
		
	}
	
	private static void initNetworkStorage() throws Exception{
		
		Commons.storage = new NetworkManager(getNetworkStorageFile());
		
	}
	//--------------------------------------------------------------
	
	
}