Search in sources :

Example 6 with DeviceInfo

use of com.wso2.mobile.mdm.api.DeviceInfo in project MDM-Android-Agent by wso2-attic.

the class EntryActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entry);
    checkNotNull(CommonUtilities.SERVER_URL, "SERVER_URL");
    checkNotNull(CommonUtilities.SENDER_ID, "SENDER_ID");
    if (CommonUtilities.DEBUG_MODE_ENABLED) {
        Log.e("SENDER ID : ", CommonUtilities.SENDER_ID);
    }
    info = new DeviceInfo(EntryActivity.this);
    context = EntryActivity.this;
    if ((info.getSdkVersion() >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) && !info.isRooted()) {
        accessFlag = true;
    } else {
        accessFlag = false;
    }
    if (!(info.getSdkVersion() > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) && info.isRooted()) {
        error = getString(R.string.device_not_compatible_error);
    } else if (info.getSdkVersion() > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        error = getString(R.string.device_not_compatible_error_os);
    } else if (info.isRooted()) {
        error = getString(R.string.device_not_compatible_error_root);
    }
    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);
    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.
    GCMRegistrar.checkManifest(this);
    // mDisplay = (TextView) findViewById(R.id.display);
    registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_MESSAGE_ACTION));
    // ImageView optionBtn = (ImageView) findViewById(R.id.option_button);
    errorMessage = (TextView) findViewById(R.id.textView1);
    errorMessage.setText(error);
    if (!accessFlag) {
        errorMessage.setVisibility(View.VISIBLE);
        showAlert(error, getResources().getString(R.string.error_authorization_failed));
    }
    /*optionBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Entry.this,DisplayDeviceInfo.class);
				intent.putExtra("from_activity_name", Entry.class.getSimpleName());
				startActivity(intent);
			}
		});*/
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        if (extras.containsKey(getResources().getString(R.string.intent_extra_regid))) {
            regId = extras.getString(getResources().getString(R.string.intent_extra_regid));
        }
    }
    if (regId == null || regId.equals("")) {
        regId = GCMRegistrar.getRegistrationId(this);
    }
    SharedPreferences mainPref = context.getSharedPreferences(getResources().getString(R.string.shared_pref_package), Context.MODE_PRIVATE);
    String success = mainPref.getString(getResources().getString(R.string.shared_pref_registered), "");
    if (success.trim().equals(getResources().getString(R.string.shared_pref_reg_success))) {
        state = true;
    }
    if (accessFlag) {
        if (state) {
            Intent intent = new Intent(EntryActivity.this, AlreadyRegisteredActivity.class);
            intent.putExtra(getResources().getString(R.string.intent_extra_regid), regId);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }
    if (CommonUtilities.DEBUG_MODE_ENABLED) {
        Log.v("REGIDDDDD", regId);
    }
    if (regId.equals("") || regId == null) {
        GCMRegistrar.register(context, CommonUtilities.SENDER_ID);
    } else {
        if (GCMRegistrar.isRegisteredOnServer(this)) {
            if (CommonUtilities.DEBUG_MODE_ENABLED) {
                Log.v("Check GCM is Registered Func", "RegisteredOnServer");
            }
        // mDisplay.append(getString(R.string.already_registered) + "\n");
        } else {
            final Context context = this;
            mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    // boolean registered = ServerUtilities.register(context, regId);
                    boolean registered = true;
                    if (!registered) {
                        GCMRegistrar.unregister(context);
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    if (CommonUtilities.DEBUG_MODE_ENABLED) {
                        Log.v("REG IDDDD", regId);
                    }
                    mRegisterTask = null;
                }
            };
            if (accessFlag) {
                mRegisterTask.execute(null, null, null);
            } else {
            // Toast.makeText(getApplicationContext(), getString(R.string.device_not_compatible_error), Toast.LENGTH_LONG).show();
            }
        }
    }
    final Context context = this;
    mRegisterTask = new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                state = ServerUtilities.isRegistered(regId, context);
            } catch (Exception e) {
                e.printStackTrace();
            // HandleNetworkError(e);
            // Toast.makeText(getApplicationContext(), "No Connection", Toast.LENGTH_LONG).show();
            }
            return null;
        }

        // declare other objects as per your need
        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(EntryActivity.this, getResources().getString(R.string.dialog_checking_reg), getResources().getString(R.string.dialog_please_wait), true);
            progressDialog.setCancelable(true);
            progressDialog.setOnCancelListener(cancelListener);
        // do initialization of required objects objects here
        }

        OnCancelListener cancelListener = new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface arg0) {
                showAlert(getResources().getString(R.string.error_connect_to_server), getResources().getString(R.string.error_heading_connection));
            }
        };

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            SharedPreferences mainPref = context.getSharedPreferences(getResources().getString(R.string.shared_pref_package), Context.MODE_PRIVATE);
            String success = mainPref.getString(getResources().getString(R.string.shared_pref_registered), "");
            if (success.trim().equals(getResources().getString(R.string.shared_pref_reg_success))) {
                state = true;
            }
            if (accessFlag) {
                if (state) {
                    Intent intent = new Intent(EntryActivity.this, AlreadyRegisteredActivity.class);
                    intent.putExtra(getResources().getString(R.string.intent_extra_regid), regId);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                // finish();
                } else {
                    mLicenseTask = new AsyncTask<Void, Void, String>() {

                        @Override
                        protected String doInBackground(Void... params) {
                            // boolean registered = ServerUtilities.register(context, regId);
                            String response = "";
                            try {
                                response = ServerUtilities.getEULA(context, "");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return response;
                        }

                        @Override
                        protected void onPostExecute(String result) {
                            if (CommonUtilities.DEBUG_MODE_ENABLED) {
                                Log.v("REG IDDDD", regId);
                            }
                            if (result != null) {
                                SharedPreferences mainPref = EntryActivity.this.getSharedPreferences(getResources().getString(R.string.shared_pref_package), Context.MODE_PRIVATE);
                                Editor editor = mainPref.edit();
                                editor.putString(getResources().getString(R.string.shared_pref_eula), result);
                                editor.commit();
                            }
                            mLicenseTask = null;
                        }
                    };
                    // mLicenseTask.execute();
                    Intent intent = new Intent(EntryActivity.this, AuthenticationActivity.class);
                    intent.putExtra(getResources().getString(R.string.intent_extra_regid), regId);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                // finish();
                }
            }
            mRegisterTask = null;
        }
    };
    if (accessFlag) {
        if (state) {
            Intent intent = new Intent(EntryActivity.this, AlreadyRegisteredActivity.class);
            intent.putExtra(getResources().getString(R.string.intent_extra_regid), regId);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else {
            mRegisterTask.execute(null, null, null);
        }
    } else {
        showAlert(getResources().getString(R.string.device_not_compatible_error), getResources().getString(R.string.error_authorization_failed));
    }
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) SharedPreferences(android.content.SharedPreferences) DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) DeviceInfo(com.wso2.mobile.mdm.api.DeviceInfo) Editor(android.content.SharedPreferences.Editor) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Example 7 with DeviceInfo

use of com.wso2.mobile.mdm.api.DeviceInfo in project MDM-Android-Agent by wso2-attic.

the class ServerUtilities method register.

public static boolean register(String regId, Context context) {
    DeviceInfo deviceInfo = new DeviceInfo(context);
    JSONObject jsObject = new JSONObject();
    String osVersion = "";
    String response = "";
    boolean state = false;
    SharedPreferences mainPref = context.getSharedPreferences(context.getResources().getString(R.string.shared_pref_package), Context.MODE_PRIVATE);
    String type = mainPref.getString(context.getResources().getString(R.string.shared_pref_reg_type), "");
    try {
        osVersion = deviceInfo.getOsVersion();
        jsObject.put("device", deviceInfo.getDevice());
        jsObject.put("imei", deviceInfo.getDeviceId());
        jsObject.put("imsi", deviceInfo.getIMSINumber());
        jsObject.put("model", deviceInfo.getDeviceModel());
        Map<String, String> params = new HashMap<String, String>();
        params.put("regid", regId);
        params.put("properties", jsObject.toString());
        params.put("email", deviceInfo.getEmail());
        params.put("osversion", osVersion);
        params.put("platform", "Android");
        params.put("vendor", deviceInfo.getDeviceManufacturer());
        params.put("type", type);
        params.put("mac", deviceInfo.getMACAddress());
        // Calls the function "sendTimeWait" to do a HTTP post to our server
        // using Android HTTPUrlConnection API
        response = sendWithTimeWait("devices/register", params, "POST", context).get("status");
        if (response.equals(CommonUtilities.REGISTERATION_SUCCESSFUL)) {
            state = true;
        } else {
            state = false;
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return state;
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) HashMap(java.util.HashMap) DeviceInfo(com.wso2.mobile.mdm.api.DeviceInfo) JSONException(org.json.JSONException)

Aggregations

DeviceInfo (com.wso2.mobile.mdm.api.DeviceInfo)7 Bundle (android.os.Bundle)5 SharedPreferences (android.content.SharedPreferences)4 Intent (android.content.Intent)3 TextView (android.widget.TextView)3 JSONException (org.json.JSONException)3 View (android.view.View)2 HashMap (java.util.HashMap)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 SuppressLint (android.annotation.SuppressLint)1 TargetApi (android.annotation.TargetApi)1 AlertDialog (android.app.AlertDialog)1 PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 OnCancelListener (android.content.DialogInterface.OnCancelListener)1 IntentFilter (android.content.IntentFilter)1 Editor (android.content.SharedPreferences.Editor)1