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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
| import android.app.ActivityManager; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Handler;
import org.json.JSONObject;
import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLDecoder; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map;
public class AuthService {
static Context mContext; static File mDir;
static String createSql = "create table user(id integer primary key autoincrement, name, id_card_number, dt, age integer, during integer)"; static String selectSql = "select id, name, id_card_number, dt, age, during from user"; static String insertSql = "insert into user(name, id_card_number, age, dt, during) values(?,?,?,?,?)"; static String updateSql = "update user set dt=?,during=? where id=?";
static String dt = null; static int id = 1; static int age = 0; public static int during = 0; static long times = 0; static Handler mHandler;
public static void init(Context context, File dir) { mContext = context; mDir = dir; SQLiteDatabase DB = null; Cursor cursor = null; Date today = new Date(); times = today.getTime() / 1000 / 60;
try { DB = SQLiteDatabase.openOrCreateDatabase(mDir + "/info.db", null); try { cursor = DB.rawQuery(selectSql, null); } catch (Exception e) { DB.execSQL(createSql); cursor = DB.rawQuery(selectSql, null); }
if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String nameString = cursor.getString(cursor.getColumnIndex("name")); String id_card_number = cursor.getString(cursor.getColumnIndex("id_card_number")); dt = cursor.getString(cursor.getColumnIndex("dt")); id = cursor.getInt(cursor.getColumnIndex("id")); age = cursor.getInt(cursor.getColumnIndex("age")); during = cursor.getInt(cursor.getColumnIndex("during"));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String dateTime = df.format(today); if (dateTime.compareTo(dt) == 0) { checkAnti(); } else { dt = dateTime; during = 0; updateUser(); } break; } } else { showAuthTipDialog(); } } catch (Exception e) { showAuthTipDialog(); } finally { if (cursor != null) cursor.close(); if (DB != null) DB.close(); }
scheduleAnti(); }
private static void showAuthTipDialog() { AuthTipDialog authTipDialog = new AuthTipDialog.Builder(mContext).create(); authTipDialog.show(); }
public static void scheduleAnti() { mHandler = new Handler(); Runnable r = new Runnable() { @Override public void run() { checkAnti(); mHandler.postDelayed(this, 300000); } }; mHandler.postDelayed(r, 300000); }
public static void saveUser(String id_card_number, String name) { SQLiteDatabase DB = null; try { DB = SQLiteDatabase.openOrCreateDatabase(mDir + "/info.db", null); Calendar cal = Calendar.getInstance(); age = cal.get(Calendar.YEAR) - new Integer(id_card_number.substring(6, 10));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); dt = df.format(new Date());
DB.execSQL(insertSql, new String[]{name, id_card_number, age + "", dt, "0"}); } catch (Exception e) { e.printStackTrace(); } finally { if (DB != null) DB.close(); } }
public static void checkAnti() { if (dt == null) return;
int duringMax = 60 * 24; boolean showTip = false;
if (age < 13) { duringMax = 60; showTip = true; } else if (age < 18) { duringMax = 60 * 2; showTip = true; }
Date today = new Date(); int h = today.getHours();
long d = today.getTime() / 1000 / 60 - times; during += d; times = today.getTime() / 1000 / 60; updateUser();
if (during >= duringMax) { AntiDialog antiDialog = new AntiDialog.Builder(mContext).create(duringMax); antiDialog.show(); } else if (duringMax == 60 && (h < 8 || h >= 21)) { AntiDialog antiDialog = new AntiDialog.Builder(mContext).create(1); antiDialog.show(); } else if (showTip) { AntiDialog antiDialog = new AntiDialog.Builder(mContext).create(0); antiDialog.show(); } }
public static void updateUser() { SQLiteDatabase DB = null; try { DB = SQLiteDatabase.openOrCreateDatabase(mDir + "/info.db", null); DB.execSQL(updateSql, new String[]{dt, during + "", id + ""}); } catch (Exception e) { e.printStackTrace(); } finally { if (DB != null) DB.close(); } }
public static boolean idmatch(String id_card_number, String name) { String access_token = getAuth(); String postIdMatchUrl = "https://aip.baidubce.com/rest/2.0/face/v3/person/idmatch?access_token=" + access_token;
try { JSONObject jsonParams = new JSONObject(); jsonParams.put("id_card_number", id_card_number); jsonParams.put("name", name); String json = jsonParams.toString();
URL realUrl = new URL(postIdMatchUrl); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setConnectTimeout(5000); connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST");
OutputStream os = connection.getOutputStream(); os.write(json.getBytes("UTF-8")); os.flush();
InputStream inputStream; int code = connection.getResponseCode(); if (code == 200) { inputStream = connection.getInputStream(); } else { inputStream = connection.getErrorStream(); return false; }
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = URLDecoder.decode(lines, "UTF-8"); sb.append(lines); }
JSONObject resultObject = new JSONObject(sb.toString()); connection.disconnect();
int error_code = resultObject.getInt("error_code"); if (error_code == 0) { saveUser(id_card_number, name); return true; } } catch (Exception e) { e.printStackTrace(); } return false; }
private static String getAuth() { String clientId = "YOUR_API_KEY"; String clientSecret = "YOUR_SECRET_KEY"; return getAuth(clientId, clientSecret); }
private static String getAuth(String ak, String sk) { String authHost = "https://aip.baidubce.com/oauth/2.0/token?"; String getAccessTokenUrl = authHost + "grant_type=client_credentials" + "&client_id=" + ak + "&client_secret=" + sk;
try { URL realUrl = new URL(getAccessTokenUrl); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setRequestMethod("GET"); connection.connect();
InputStream inputStream; int code = connection.getResponseCode(); if (code == 200) { inputStream = connection.getInputStream(); } else { inputStream = connection.getErrorStream(); }
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); String result = ""; String line; while ((line = in.readLine()) != null) { result += line; }
JSONObject jsonObject = new JSONObject(result); return jsonObject.getString("access_token"); } catch (Exception e) { e.printStackTrace(); } return null; }
public static void exitApp() { ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks(); for (ActivityManager.AppTask appTask : appTaskList) { appTask.finishAndRemoveTask(); } android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } }
|