| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380 |
- package com.usai.util;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.security.KeyManagementException;
- import java.security.KeyStore;
- import java.security.KeyStoreException;
- import java.security.NoSuchAlgorithmException;
- import java.security.UnrecoverableKeyException;
- import java.util.Iterator;
- import java.util.Set;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.HttpVersion;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ClientConnectionManager;
- import org.apache.http.conn.ConnectTimeoutException;
- import org.apache.http.conn.scheme.PlainSocketFactory;
- import org.apache.http.conn.scheme.Scheme;
- import org.apache.http.conn.scheme.SchemeRegistry;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.entity.mime.HttpMultipartMode;
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.entity.mime.content.StringBody;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpConnectionParams;
- import org.apache.http.params.HttpParams;
- import org.apache.http.params.HttpProtocolParams;
- import org.apache.http.protocol.HTTP;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.usai.apex.ApexTrackingApplication;
- import com.usai.apex.CustomizeFieldsActivity.fieldedit;
- import android.content.Context;
- import android.content.SharedPreferences.Editor;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteStatement;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.util.Log;
- public class Network
- {
- public static final int BEHAVIOR_SEARCH = 0;
- public static final int BEHAVIOR_RESULT = 1;
- private static final int REQUEST_TIMEOUT = 60 * 1000; // request
- // time
- // out
- // 20
- // secs
- private static final int SO_TIMEOUT = 60 * 1000; // so
- // time
- // out
- // 20
- // secs;
- public static int AP_USER_AUTH = 1;
- public static int AP_USER_NOT_AUTH = 2;
- public static int AP_USER_NOT_EXIST = 3;
- public static int AP_SESSION_EXPIRED = 4;
- public static int AP_MESSAGE_NEW = 5;
- public static int AP_MESSAGE_NONE = 6;
- //
- public static final int RESULT_FALSE = 0;
- public static final int RESULT_TRUE = -1;
- public static final int RESULT_NET_ERROR = -3;
- public static final int RESULT_NET_NOTAVAILABLE = -4;
- public static final int RESULT_ERROR = -5;
- public static final int RESULT_LOCALFILE_ERROR = -7;
- public static final int RESULT_USERAUTH_ERROR = -9;
- public static final int RESULT_UPDATE_USERAUTH_ERROR = -11;
- public static final int RESULT_SESSION_EXPIRED = -13;
- public static final int RESULT_VER_LOW = -15;
- public static String URL_UPDATE_AUTH = "https://ra.apexshipping.com/login.php";
- public static String URL_REQUEST_COUNT = "https://ra.apexshipping.com/main.php";
- public static String URL_REQUEST_RECORDS = "https://ra.apexshipping.com/main.php";
- public static String URL_RETRIEVE_PASS = "https://ra.apexshipping.com/main.php";
- public static String URL_ANNOUNCEMENTS = "https://ra.apexshipping.com/mobile_news.php";
- public static String URL_NEWS = "https://ra.apexshipping.com/mobile_news.php";
- public static String URL_LOCATIONS = "https://ra.apexshipping.com/mobile_news.php";
- public static String URL_PUSH = "https://ra.apexshipping.com/main.php";
- private static HttpClient getNewHttpClient()
- {
- try
- {
- KeyStore trustStore = KeyStore.getInstance(KeyStore
- .getDefaultType());
- trustStore.load(null, null);
- SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- HttpParams params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
- HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT);
- HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", PlainSocketFactory
- .getSocketFactory(), 80));
- registry.register(new Scheme("https", sf, 443));
- ClientConnectionManager ccm = new ThreadSafeClientConnManager(
- params, registry);
- return new DefaultHttpClient(ccm, params);
- }
- catch (Exception e)
- {
- return new DefaultHttpClient();
- }
- }
- public static String get_marketnews(String id, int limit)
- {
- String TAG = "net_dbg@get_marketnews";
- if (!Network.NetworkIsAvailable())
- {
- Log.d(TAG, "network not available!");
- return null; // network not available
- }
- Bundle parms = new Bundle();
- parms.putString("module_name", "Market News");
- parms.putString("action_type", "search");
- parms.putString("id", id + "");
- parms.putString("limit", limit + "");
- String jstr = getJson(Network.URL_NEWS, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- return jstr;
- }
- public static String get_servicelocation()
- {
- String TAG = "net_dbg@get_servicelocation";
- if (!Network.NetworkIsAvailable())
- {
- Log.d(TAG, "network not available!");
- return null; // network not available
- }
- Bundle parms = new Bundle();
- parms.putString("module_name", "Location");
- parms.putString("action_type", "search");
- // parms.putString("id", id+"");
- parms.putString("limit", "999");
- String jstr = getJson(Network.URL_LOCATIONS, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- return jstr;
- }
- public static String get_announcements(String id, int limit)
- {
- String TAG = "net_dbg@get_announcements";
- if (!Network.NetworkIsAvailable())
- {
- Log.d(TAG, "network not available!");
- return null; // network not available
- }
- Bundle parms = new Bundle();
- parms.putString("module_name", "Announcements");
- parms.putString("action_type", "search");
- parms.putString("id", id);
- parms.putString("limit", limit + "");
- String jstr = getJson(Network.URL_ANNOUNCEMENTS, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- return jstr;
- }
- public static int change_pass(String oldpass, String newpass, String user)
- {
- String TAG = "net_dbg@change_pass";
- Log.d(TAG, "old:" + oldpass + ";new:" + newpass);
- if (!Network.NetworkIsAvailable())
- {
- Log.d(TAG, "network not available!");
- return Network.RESULT_NET_NOTAVAILABLE; // network not available
- }
- Bundle parms = new Bundle();
- parms.putString("user", user);
- parms.putString("oldpass", oldpass);
- parms.putString("newpass", newpass);
- parms.putString("module_name", "Change Password");
- parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
- parms.putString("action", "handset_search");
- String jstr = getJson(Network.URL_RETRIEVE_PASS, parms);
- /*
- * error occur while get authorization info from server. include can not
- * reach server , wrong parms ,server get wrong , etc.
- */
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return Network.RESULT_NET_ERROR;
- }
- JSONObject jsobj;
- try
- {
- jsobj = new JSONObject(jstr);
- if (jsobj.length() > 0)
- {
- int iresult = jsobj.getInt("result");
- if (iresult == Network.AP_SESSION_EXPIRED)
- {
- Log.e(TAG, "AP_SESSION_EXPIRED");
- Network.get_Auth(user, oldpass);
- jstr = getJson(Network.URL_RETRIEVE_PASS, parms);
- jsobj = new JSONObject(jstr);
- iresult = jsobj.getInt("result");
- }
- if (iresult != Network.AP_USER_AUTH)
- {
- // user not authorized return
- Log.d(TAG,
- "USER NOT AUTHORIZED CODE="
- + jsobj.getInt("result"));
- return RESULT_FALSE;
- }
- else
- {
-
- return RESULT_TRUE;
- }
- }
- return RESULT_NET_ERROR;
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return RESULT_NET_ERROR;
- }
- public static int retrieve_pass(String user, String email)
- {
- String TAG = "net_dbg@retrieve_pass";
- Log.d(TAG, "u:" + user + ";e:" + email);
- if (!Network.NetworkIsAvailable())
- {
- Log.d(TAG, "network not available!");
- return Network.RESULT_NET_NOTAVAILABLE; // network not available
- }
- Bundle parms = new Bundle();
- parms.putString("user", user);
- parms.putString("email", email);
- parms.putString("module_name", "Retrieve Password");
- parms.putString("action", "handset_search");
- String jstr = getJson(Network.URL_RETRIEVE_PASS, parms);
- /*
- * error occur while get authorization info from server. include can not
- * reach server , wrong parms ,server get wrong , etc.
- */
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return Network.RESULT_NET_ERROR;
- }
- JSONObject jsobj;
- // array = new JSONArray(json);
- try
- {
- jsobj = new JSONObject(jstr);
- if (jsobj.length() > 0)
- {
- int iresult = jsobj.getInt("result");
- if (iresult != Network.AP_USER_AUTH)
- {
- // user not authorized return
- Log.d(TAG,
- "USER NOT AUTHORIZED CODE="
- + jsobj.getInt("result"));
- return RESULT_FALSE;
- }
- return RESULT_TRUE;
- }
- return RESULT_NET_ERROR;
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return RESULT_NET_ERROR;
- }
- public static String getJson(String url, Bundle parms)
- {
- String TAG = "net_dbg@GetJson";
- Log.d(TAG, "entry");
- parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
- // if (true)
- // return fakegetJson(url);
- try
- {
- // BasicHttpParams httpParams = new BasicHttpParams();
- // HttpConnectionParams.setConnectionTimeout(httpParams,
- // REQUEST_TIMEOUT);
- // HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
- HttpClient client = getNewHttpClient();// new
- // DefaultHttpClient(httpParams);
- HttpPost post = new HttpPost(url);
- MultipartEntity reqEntity = new MultipartEntity(
- HttpMultipartMode.BROWSER_COMPATIBLE);
- Set<String> keys = parms.keySet();
- Log.d(TAG, "================parms============");
- for (String key : keys)
- {
- if (key.contains("_file"))
- {
- ;
- }
- else
- reqEntity.addPart(key, new StringBody(parms.get(key)
- .toString()));
- Log.d(TAG, "key=" + key + " val="
- + parms.get(key).toString());
- }
- Log.d(TAG, "================parms============");
- post.setEntity(reqEntity);
- HttpResponse response = client.execute(post);
- int statucode = response.getStatusLine().getStatusCode();
- if (statucode == HttpStatus.SC_OK)
- {
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null)
- {
- Log.d(TAG,
- "Response: content len==>"
- + resEntity.getContentLength());
- InputStream is = resEntity.getContent();
- try
- {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(is, "utf-8"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = br.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- Log.d(TAG, "Response: content begin");
- Log.d(TAG, sb.toString());
- Log.d(TAG, "Response: content end");
- if (sb.length() <= 0)
- {
- return null;
- }
- return sb.toString();
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return null;
- // TODO: handle exception
- }
- finally
- {
- is.close();
- }
- }
- else
- {
- /*
- * resEntity is null
- */
- Log.d(TAG, "RESPONSE ENTITY IS NULL");
- return null;
- }
- }
- else
- {
- /*
- * Http error; out put error code;
- */
- Log.d(TAG, "HTTP " + statucode);
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null)
- {
- InputStream is = resEntity.getContent();
- try
- {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(is, "utf-8"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = br.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- Log.d(TAG, "Response: content begin");
- Log.d(TAG, sb.toString());
- Log.d(TAG, "Response: content end");
- return null;
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return null;
- // TODO: handle exception
- }
- finally
- {
- is.close();
- }
- }
- else
- {
- /*
- * resEntity is null
- */
- Log.e(TAG, "RESPONSE ENTITY IS NULL");
- return null;
- }
- }
- }
- catch (ConnectTimeoutException e)
- {
- Log.d(TAG, "request time out");
- return null;
- }
- catch (Exception e)
- {
- Log.d(TAG, e.toString());
- return null;
- }
- }
- public static int get_recordcount(Bundle parms)
- {
- String TAG = "net_dbg@get_recordcount";
- parms.putString("action", "handset_search_count");
- parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
- Log.d(TAG, "sessionid=" + ApexTrackingApplication.get_sessionid());
- String jstr = getJson(Network.URL_REQUEST_COUNT, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return Network.RESULT_NET_ERROR;
- }
- JSONObject jsobj;
- // array = new JSONArray(json);
- try
- {
- jsobj = new JSONObject(jstr);
- if (jsobj.length() > 0)
- {
- int iresult = jsobj.getInt("result");
- if (iresult == Network.AP_SESSION_EXPIRED)
- {
- Log.e(TAG, "AP_SESSION_EXPIRED");
- String user, pass;
- user = ApexTrackingApplication.get_user();
- pass = ApexTrackingApplication.get_pass();
- Network.get_Auth(user, pass);
- jstr = getJson(Network.URL_REQUEST_COUNT, parms);
- jsobj = new JSONObject(jstr);
- iresult = jsobj.getInt("result");
- }
- if (iresult != Network.AP_USER_AUTH)
- {
- // session expired
- Log.d(TAG,
- "USER NOT AUTHORIZED CODE="
- + jsobj.getInt("result"));
- return RESULT_SESSION_EXPIRED;
- // if (get_Auth(name, password) == RESULT_TRUE)
- // {
- // return get_recordcount(name, password, parms);
- //
- // }
- // else
- // {
- //
- // Log.d(TAG,
- // "USER NOT AUTHORIZED CODE="
- // + jsobj.getInt("result"));
- // return RESULT_SESSION_EXPIRED;
- // }
- }
- else
- {
- ApexTrackingApplication.SetAuthorizeStatus(true);
- return jsobj.getInt("count");
- }
- }
- else
- {
- Log.d(TAG, "json is wrong");
- return Network.RESULT_NET_ERROR;
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return RESULT_ERROR;
- }
- public static String get_records(String name, String password, Bundle parms)
- {
- String TAG = "net_dbg@get_records";
- // parms.putString("user", name);
- // parms.putString("password", password);
- // if (!Network.NetworkIsAvailable())
- // {
- // Log.d(TAG, "network not available!");
- // return "Network.RESULT_NET_NOTAVAILABLE"; // network not available
- // }
- parms.putString("action", "handset_search");
- parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
- String jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- JSONObject jsobj;
- //
- // array = new JSONArray(json);
- try
- {
- jsobj = new JSONObject(jstr);
- if (jsobj.length() > 0)
- {
- int iresult = jsobj.getInt("result");
- if (iresult == Network.AP_SESSION_EXPIRED)
- {
- Log.e(TAG, "AP_SESSION_EXPIRED");
- String user, pass;
- user = ApexTrackingApplication.get_user();
- pass = ApexTrackingApplication.get_pass();
- Network.get_Auth(user, pass);
- jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
- jsobj = new JSONObject(jstr);
- iresult = jsobj.getInt("result");
- }
- if (iresult != Network.AP_USER_AUTH)
- {
- // session expired
- Log.d(TAG,
- "USER NOT AUTHORIZED CODE="
- + jsobj.getInt("result"));
- return null;
- // if (get_Auth(name, password) == RESULT_TRUE)
- // {
- // return get_records(name, password, parms);
- //
- // }
- // else
- // {
- //
- // Log.d(TAG,
- // "USER NOT AUTHORIZED CODE="
- // + jsobj.getInt("result"));
- // return null;
- // }
- }
- else
- {
- ApexTrackingApplication.SetAuthorizeStatus(true);
- return jstr;
- }
- }
- else
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- // return RESULT_ERROR;
- }
- public static String get_push(String e_id, String s_id, int limit,
- int offset)
- {
- String TAG = "net_dbg@get_push";
- Bundle parms = new Bundle();
- parms.putString("action", "handset_search");
- parms.putString("e_id", e_id);
- parms.putString("s_id", s_id);
- parms.putString("limit", limit + "");
- parms.putString("offset", offset + "");
- parms.putString("action_type", "detail");
- parms.putString("module_name", "Pull Message");
- parms.putString("uuid", commonUtil.getDUID());
- String sessionid = ApexTrackingApplication.get_sessionid();
- // if (TextUtils.isEmpty(sessionid))
- // {
- //
- // Log.d(TAG, "no sessionid return!");
- // return null;
- //
- // }
- String user = ApexTrackingApplication.get_user();
- // if (TextUtils.isEmpty(user))
- // {
- //
- // Log.d(TAG, "no user name return!");
- // return null;
- //
- // }
- parms.putString("sessionid", sessionid);
- parms.putString("user", user);
- // SQLiteDatabase db =
- // dbUtil.OpenDB(ApexTrackingApplication.get_instance(), null, false);
- // Cursor cursor = db.query("push_message", new String[] {"MAX(e_id)"},
- // "user = '"+ApexTrackingApplication.get_user()+"'", null, null, null,
- // null, null);
- // if (cursor.moveToNext())
- // {
- // String maxid = cursor.getString(0);
- // if(!TextUtils.isEmpty(maxid))
- // parms.putString("id", maxid);
- // }
- // dbUtil.CloseCursor(cursor);
- // dbUtil.CloseDB(db);
- String jstr = getJson(Network.URL_PUSH, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- JSONObject jsobj;
- //
- // array = new JSONArray(json);
- try
- {
- jsobj = new JSONObject(jstr);
- if (jsobj.length() > 0)
- {
- int iresult = jsobj.getInt("result");
- if (iresult == Network.AP_SESSION_EXPIRED)
- {
- Log.e(TAG, "AP_SESSION_EXPIRED");
- String pass;
- pass = ApexTrackingApplication.get_pass();
- Network.get_Auth(user, pass);
- jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
- jsobj = new JSONObject(jstr);
- iresult = jsobj.getInt("result");
- }
- if (iresult != Network.AP_MESSAGE_NEW)
- {
- // session expired
- Log.d(TAG,
- "DOSE NOT GET NEW MESSAGE CODE="
- + jsobj.getInt("result"));
- return null;
- // if (get_Auth(name, password) == RESULT_TRUE)
- // {
- // return get_records(name, password, parms);
- //
- // }
- // else
- // {
- //
- // Log.d(TAG,
- // "USER NOT AUTHORIZED CODE="
- // + jsobj.getInt("result"));
- // return null;
- // }
- }
- else
- {
- return jstr;
- }
- }
- else
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- // return RESULT_ERROR;
- }
- public static String check_push()
- {
- String TAG = "net_dbg@check_push";
- Bundle parms = new Bundle();
- parms.putString("action", "handset_search");
- parms.putString("module_name", "Pull Message");
- parms.putString("uuid", commonUtil.getDUID());
- String sessionid = ApexTrackingApplication.get_sessionid();
- // if (TextUtils.isEmpty(sessionid))
- // {
- //
- // Log.d(TAG, "no sessionid return!");
- // return null;
- //
- // }
- String user = ApexTrackingApplication.get_user();
- if (TextUtils.isEmpty(user))
- {
- Log.d(TAG, "no user name return!");
- return null;
- }
- // parms.putString("sessionid", sessionid);
- parms.putString("user", user);
- SQLiteDatabase db = dbUtil.OpenDB(
- ApexTrackingApplication.get_instance(), null, false);
- Cursor cursor = db.query("push_message", new String[] { "MAX(e_id)" },
- "user = '" + ApexTrackingApplication.get_user() + "'", null,
- null, null, null, null);
- if (cursor.moveToNext())
- {
- String maxid = cursor.getString(0);
- if (!TextUtils.isEmpty(maxid))
- parms.putString("id", maxid);
- }
- dbUtil.CloseCursor(cursor);
- dbUtil.CloseDB(db);
- String jstr = getJson(Network.URL_PUSH, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- JSONObject jsobj;
- //
- // array = new JSONArray(json);
- try
- {
- jsobj = new JSONObject(jstr);
- if (jsobj.length() > 0)
- {
- int iresult = jsobj.getInt("result");
- if (iresult == Network.AP_SESSION_EXPIRED)
- {
- Log.e(TAG, "AP_SESSION_EXPIRED");
- String pass;
- pass = ApexTrackingApplication.get_pass();
- Network.get_Auth(user, pass);
- jstr = getJson(Network.URL_PUSH, parms);
- jsobj = new JSONObject(jstr);
- iresult = jsobj.getInt("result");
- }
- if (iresult != Network.AP_MESSAGE_NEW)
- {
- // session expired
- Log.d(TAG,
- "DOSE NOT GET NEW MESSAGE CODE="
- + jsobj.getInt("result"));
- return null;
- // if (get_Auth(name, password) == RESULT_TRUE)
- // {
- // return get_records(name, password, parms);
- //
- // }
- // else
- // {
- //
- // Log.d(TAG,
- // "USER NOT AUTHORIZED CODE="
- // + jsobj.getInt("result"));
- // return null;
- // }
- }
- else
- {
- return jstr;
- }
- }
- else
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- // return RESULT_ERROR;
- }
- public static String get_detail(Bundle parms)
- {
- String TAG = "net_dbg@get_detail";
- parms.putString("action", "handset_search");
- parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
- String jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- JSONObject jsobj;
- //
- // array = new JSONArray(json);
- try
- {
- jsobj = new JSONObject(jstr);
- if (jsobj.length() > 0)
- {
- int iresult = jsobj.getInt("result");
- if (iresult == Network.AP_SESSION_EXPIRED)
- {
- Log.e(TAG, "AP_SESSION_EXPIRED");
- String user, pass;
- user = ApexTrackingApplication.get_user();
- pass = ApexTrackingApplication.get_pass();
- Network.get_Auth(user, pass);
- jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
- jsobj = new JSONObject(jstr);
- iresult = jsobj.getInt("result");
- }
- if (iresult != Network.AP_USER_AUTH)
- {
- // session expired
- Log.d(TAG,
- "USER NOT AUTHORIZED CODE="
- + jsobj.getInt("result"));
- return null;
- // if (get_Auth(name, password) == RESULT_TRUE)
- // {
- // return get_records(name, password, parms);
- //
- // }
- // else
- // {
- //
- // Log.d(TAG,
- // "USER NOT AUTHORIZED CODE="
- // + jsobj.getInt("result"));
- // return null;
- // }
- }
- else
- {
- ApexTrackingApplication.SetAuthorizeStatus(true);
- return jstr;
- }
- }
- else
- {
- Log.d(TAG, "json is wrong");
- return null;
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- // return RESULT_ERROR;
- }
- public static int get_Auth(String name, String password)
- {
- String TAG = "net_dbg@get_Auth";
- Log.d(TAG, "u:" + name + ";p:" + password);
- if (!Network.NetworkIsAvailable())
- {
- Log.d(TAG, "network not available!");
- return Network.RESULT_NET_NOTAVAILABLE; // network not available
- }
- Bundle parms = new Bundle();
- parms.putString("user", name);
- parms.putString("password", password);
- parms.putString("action", "handset_login");
- int ver = ApexTrackingApplication
- .get_instance()
- .getSharedPreferences(name + "_Apex_auth", Context.MODE_PRIVATE)
- .getInt("AuthInfoVer", 0);
- parms.putString("auth_ver", ver + "");
- String jstr = getJson(Network.URL_UPDATE_AUTH, parms);
- /*
- * error occur while get authorization info from server. include can not
- * reach server , wrong parms ,server get wrong , etc.
- */
- if (jstr == null || jstr.length() <= 0)
- {
- Log.d(TAG, "json is wrong");
- return Network.RESULT_NET_ERROR;
- }
- return parse_authinfo(jstr, name, password);
- }
- private static int parse_authinfo(String json, String user, String pass)
- {
- String TAG = "net_dbg@parse_authinfo";
- Log.d(TAG, json);
- // JSONArray array;
- JSONObject jsobj;
- SQLiteDatabase db = null;
- try
- {
- // array = new JSONArray(json);
- jsobj = new JSONObject(json);
- if (jsobj.length() > 0)
- {
- // JSONObject objresult = jsobj.get.getJSONObject(0);// result
- // the
- // server result
- // ;
- if (jsobj.getInt("result") != Network.AP_USER_AUTH)
- {
- ApexTrackingApplication.logout();
- // user not authorized return
- Log.d(TAG,
- "USER NOT AUTHORIZED CODE="
- + jsobj.getInt("result"));
- return RESULT_FALSE;
- }
- JSONObject objheader = jsobj.getJSONObject("header");
- String required_ver = objheader.getString("client_ver");
- String current_ver = ApexTrackingApplication.get_instance()
- .getPackageManager().getPackageInfo("com.usai.apex", 0).versionName;
- if (current_ver.compareTo(required_ver) < 0)
- return RESULT_VER_LOW;
- // JSONObject objsessionid = jsobj.getJSONObject(1); // session
- // id
- // on
- // server;
- // ApexTrackingApplication.put_sessionid(objheader
- // .getString("sessionid"));
- // ApexTrackingApplication.put_user(user);
- // ApexTrackingApplication.put_password(pass);
- // ApexTrackingApplication.put_authorization(true);
- ApexTrackingApplication.login(objheader.getString("sessionid"),
- user, pass);
- Log.d(TAG, "sessionid=" + objheader.getString("sessionid"));
- // JSONObject objupdate = array.getJSONObject(3); // whether
- // need
- // update ;
- if (objheader.getBoolean("update") == false)
- {
- // no update on the server;
- return RESULT_TRUE;
- }
- // JSONObject objver = array.getJSONObject(4); // new version
- // get
- // from server;
- Editor editor = ApexTrackingApplication
- .get_instance()
- .getSharedPreferences(user + "_Apex_auth",
- Context.MODE_PRIVATE).edit();
- editor.putInt("AuthInfoVer", objheader.getInt("ver"));
- JSONObject objfuncs = jsobj.getJSONObject("functions");
- Iterator<?> it = objfuncs.keys();
- db = dbUtil.OpenDB(ApexTrackingApplication.get_instance(),
- null, true);
- String sql = "insert into fields_info(name,aname,field_type,function_name,behavior,priority,show,user) values(?,?,?,?,?,?,?,?)";
- String sql1 = "insert into actions_info(name,function_name,user,priority) values(?,?,?,?)";
- SQLiteStatement stat = db.compileStatement(sql);
- SQLiteStatement stat1 = db.compileStatement(sql1);
- db.beginTransaction();
- db.execSQL("update fields_info set abandon = 1 where user ='"
- + user + "'");
- db.execSQL("update actions_info set abandon = 1 where user ='"
- + user + "'");
- String field_name, field_type, alias_name;
- while (it.hasNext()) // loop for each function
- {
- String func_name = (String) it.next();
- // Set<String> funset = new HashSet<String>();
- JSONObject objfun = objfuncs.getJSONObject(func_name);
- int behavior, priority = 999;
- int show = 1;
- JSONObject objbehavior = objfun.getJSONObject("search");
- Iterator<?> itbehavior = objbehavior.keys();
- behavior = BEHAVIOR_SEARCH;
- while (itbehavior.hasNext()) // loop for search fields in
- // certain function
- {
- String field = (String) itbehavior.next();
- JSONObject field_info = objbehavior
- .getJSONObject(field);
- field_name = field_info.getString("name");
- field_type = field_info.getString("type");
- alias_name = field_info.getString("alias");
- priority = field_info.getInt("priority");
- if (priority == 999)
- show = 0;
- else
- show = 1;
- int id = dbUtil.get_recordid(db, "fields_info",
- "name='" + field_name + "' and behavior="
- + behavior + " and function_name='"
- + func_name + "' and user='" + user
- + "'");
- if (id >= 0) // record exist;
- {
- db.execSQL("update fields_info set abandon = 0 where _id ="
- + id);
- }
- else
- {
- stat.bindString(1, field_name);
- stat.bindString(2, alias_name);
- stat.bindString(3, field_type);
- stat.bindString(4, func_name);
- stat.bindLong(5, behavior);
- stat.bindLong(6, priority);
- stat.bindLong(7, show);
- stat.bindString(8, user);
- stat.executeInsert();
- }
- }
- objbehavior = objfun.getJSONObject("result");
- Iterator<?> itresult = objbehavior.keys();
- behavior = BEHAVIOR_RESULT;
- while (itresult.hasNext()) // loop for result fields in
- // certain function
- {
- String field = (String) itresult.next();
- JSONObject field_info = objbehavior
- .getJSONObject(field);
- field_name = field_info.getString("name");
- field_type = field_info.getString("type");
- alias_name = field_info.getString("alias");
- priority = field_info.getInt("priority");
- if (priority == 999)
- show = 0;
- else
- show = 1;
- int id = dbUtil.get_recordid(db, "fields_info",
- "name='" + field_name + "' and behavior="
- + behavior + " and function_name='"
- + func_name + "' and user='" + user
- + "'");
- if (id >= 0) // record exist;
- {
- db.execSQL("update fields_info set abandon = 0 where _id ="
- + id);
- }
- else
- {
- stat.bindString(1, field_name);
- stat.bindString(2, alias_name);
- stat.bindString(3, field_type);
- stat.bindString(4, func_name);
- stat.bindLong(5, behavior);
- stat.bindLong(6, priority);
- stat.bindLong(7, show);
- stat.bindString(8, user);
- stat.executeInsert();
- }
- }
- // objfun.has("actions")
- objbehavior = objfun.getJSONObject("actions");
- Iterator<?> itactions = objbehavior.keys();
- // behavior = BEHAVIOR_RESULT;
- while (itactions.hasNext()) // loop for result fields in
- // certain function
- {
- String actionname = (String) itactions.next();
- int action_priority = objbehavior.getInt(actionname);
- int id = dbUtil.get_recordid(db, "actions_info",
- "name='" + actionname + "' and function_name='"
- + func_name + "' and user='" + user
- + "'");
- if (id >= 0) // record exist;
- {
- db.execSQL("update actions_info set abandon = 0 , priority="
- + action_priority + " where _id =" + id);
- }
- else
- {
- stat1.bindString(1, actionname);
- stat1.bindString(2, func_name);
- stat1.bindString(3, user);
- stat1.bindLong(4, action_priority);
- stat1.executeInsert();
- }
- }
- }
- db.execSQL("delete from fields_info where abandon = 1");
- db.execSQL("delete from actions_info where abandon = 1");
- db.setTransactionSuccessful();
- db.endTransaction();
- editor.commit();
- return RESULT_TRUE;
- }
- Log.d(TAG, "json is wrong");
- return RESULT_USERAUTH_ERROR;
- }
- catch (JSONException e1)
- {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- Log.d(TAG, "json is wrong");
- }
- catch (NameNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally
- {
- dbUtil.CloseDB(db);
- }
- // try
- // {
- // m_RemoteVerCode = Integer
- // .parseInt(obj.getString("verCode"));
- // m_RemoteVerName = obj.getString("verName");
- // m_NewVerUrl = obj.getString("URL");
- // }
- // catch (Exception e)
- // {
- // m_RemoteVerCode = -1;
- // m_RemoteVerName = "";
- // m_NewVerUrl = "";
- // dbgUtil.Log(Log.INFO,TAG, "download failed json file wrong!");
- // return false;
- // }
- Log.d(TAG, "json is wrong");
- return RESULT_USERAUTH_ERROR;
- }
- // private void writeData(JSONObject allData)
- // {
- //
- // File file = new File(Environment.getExternalStorageDirectory()
- // .toString() + File.separator + "live.txt");
- // if (!file.exists())
- // {
- // try
- // {
- // file.createNewFile();
- // }
- // catch (IOException e)
- // {
- // e.printStackTrace();
- // }
- // }
- // FileOutputStream fos;
- // OutputStreamWriter osw = null;
- // try
- // {
- // fos = new FileOutputStream(file);
- //
- // osw = new OutputStreamWriter(fos, "UTF-8");
- // osw.write(allData.toString());
- // osw.flush();
- // }
- // catch (FileNotFoundException e)
- // {
- // e.printStackTrace();
- // }
- // catch (UnsupportedEncodingException e)
- // {
- // e.printStackTrace();
- // }
- // catch (IOException e)
- // {
- // e.printStackTrace();
- // }
- // finally
- // {
- // if (osw != null)
- // {
- // try
- // {
- // osw.close();
- // }
- // catch (IOException e)
- // {
- // e.printStackTrace();
- // }
- // }
- // }
- // }
- public static boolean NetworkIsAvailable()// Context context)
- {
- String TAG = "net_dbg@CheckNetwork";
- ConnectivityManager connManager = (ConnectivityManager) ApexTrackingApplication
- .get_instance().getSystemService(Context.CONNECTIVITY_SERVICE);
- // .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
- if (networkInfo == null)
- {
- Log.d(TAG, "can not get Active NetworkInfo!");
- // dbgUtil.Log(Log.DEBUG, "Current Network info",
- // "can not get Active NetworkInfo!");
- return false;
- }
- NetworkInfo.State netState = networkInfo.getState();
- if (netState != NetworkInfo.State.CONNECTED)
- {
- Log.d(TAG, "not Connected!State=" + netState);
- // dbgUtil.Log(Log.DEBUG, "Current Network info",
- // "not Connected!State=" + netState);
- return false;
- }
- // int iconntype = -1;
- // iconntype = networkInfo.getType();
- // boolean bUseMobileNetwork = context.getSharedPreferences(
- // "PhoneAsstPref", 0).getBoolean("UseMobileNetwork", false);
- //
- // if (bUseMobileNetwork == false
- // && iconntype != ConnectivityManager.TYPE_WIFI && iconntype != 9/*
- // earthnet */)
- // {
- // Log.d(TAG,);
- // dbgUtil.Log(Log.DEBUG, "Current Network info",
- // "not allowed!Connection type=" + networkInfo.getTypeName());
- // return false;
- // }
- boolean bavailable = networkInfo.isAvailable();
- String strtype = networkInfo.getTypeName();
- Log.d(TAG, " type = " + strtype + " abailable = " + bavailable
- + " state " + netState);
- // dbgUtil.Log(Log.INFO, "Current Network info", " type = " + strtype
- // + " abailable = " + bavailable + " state " + netState);
- return bavailable;
- }
- public static class SSLSocketFactoryEx extends SSLSocketFactory
- {
- SSLContext sslContext = SSLContext.getInstance("TLS");
- public SSLSocketFactoryEx(KeyStore truststore)
- throws NoSuchAlgorithmException, KeyManagementException,
- KeyStoreException, UnrecoverableKeyException
- {
- super(truststore);
- TrustManager tm = new X509TrustManager()
- {
- public java.security.cert.X509Certificate[] getAcceptedIssuers()
- {
- return null;
- }
- @Override
- public void checkClientTrusted(
- java.security.cert.X509Certificate[] chain,
- String authType)
- throws java.security.cert.CertificateException
- {
- }
- @Override
- public void checkServerTrusted(
- java.security.cert.X509Certificate[] chain,
- String authType)
- throws java.security.cert.CertificateException
- {
- }
- };
- sslContext.init(null, new TrustManager[] { tm }, null);
- }
- @Override
- public Socket createSocket(Socket socket, String host, int port,
- boolean autoClose) throws IOException, UnknownHostException
- {
- return sslContext.getSocketFactory().createSocket(socket, host,
- port, autoClose);
- }
- @Override
- public Socket createSocket() throws IOException
- {
- return sslContext.getSocketFactory().createSocket();
- }
- }
- }
|