Java tutorial
package kr.go.hf.hfappandroid.app.intro; import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.widget.ImageView; import android.widget.Toast; import com.extrus.appdefence.AAPToolkit; import com.extrus.appdefence.CheckRooting; import com.rimesoft.unisafecrs.toolkit.CRSToolkit; import java.util.HashMap; import java.util.Timer; import java.util.TimerTask; import kr.go.hf.hfappandroid.app.appDefence.AppDefenceUI; import kr.go.hf.hfappandroid.app.common.AppValidateCheckHandler; import kr.go.hf.hfappandroid.app.common.Common; import kr.go.hf.hfappandroid.app.common.Constants; import kr.go.hf.hfappandroid.app.main.ErrorActivity; import kr.go.hf.hfappandroid.app.main.MainActivity; import kr.go.hf.hfappandroid.app.preference.PreferenceUtils; import kr.go.hf.hfappandroid.plugin.common.CLog; import kr.go.hf.hfappandroid.plugin.common.PluginPreference; import kr.go.hf.hfappandroid.plugin.extrusUtils.E2E; import kr.go.hf.hfappandroid.plugin.utils.Utils; import kr.go.hf.hfappandroid.R; import kr.go.hf.hfappandroid.plugin.http.HttpCommon; public class IntroActivity extends Activity { private static final String TAG = "IntroActivity"; private static final int REQUEST_START_VGUARD = 0; private static final int LOGIN_CODE_DEFAULT = 0; private static final int LOGIN_CODE_SUCCESS = 1; private static final int LOGIN_CODE_FAIL = 2; private static final int LOGIN_CODE_ERROR = 3; boolean writePermission; boolean phonePermission; boolean callPermission; private int mLoginCode = LOGIN_CODE_DEFAULT; private boolean mErrorFlag = false; private boolean mE2EFlag = false; private Intent mIntent; private Timer mIntroTimer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mIntent = new Intent(); // UniSafeCRS if (CRSToolkit.setReportURI(Constants.UNISAFE_CRS_URI, this, Constants.UNISAFE_CRS_LIC) == false) { CLog.e(TAG, "UniSafeCRS LICENSE ERROR"); } // add custom data (max 10); CRSToolkit.addCRSCustomData("IntroActivity.onCreate()"); // APP ?????(BSJ 13/05/07) ? ? ? 2015-02-25 //AAPToolkit.i(this, false, false); //OS 6.0() ??? ? - ? ? ? /*if(ContextCompat.checkSelfPermission( this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED){ phonePermission=true; } if(ContextCompat.checkSelfPermission( this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED){ callPermission=true; } if(ContextCompat.checkSelfPermission( this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){ writePermission=true; } if(!phonePermission && !callPermission && !writePermission){ //permission? enable? ? ? check .. requestPermission(); } else { startVGuard(); }*/ //for dev, ? ? 2015-02-25 mIntent.setClass(IntroActivity.this, MainActivity.class); startActivity(mIntent); } private void requestPermission() { //? permission? dialog ? ? ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 200); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); // permission ?? ?? ? ? if (requestCode == 200 && grantResults.length > 0) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { phonePermission = true; } if (grantResults[1] == PackageManager.PERMISSION_GRANTED) { callPermission = true; } if (grantResults[2] == PackageManager.PERMISSION_GRANTED) { writePermission = true; } if (phonePermission && callPermission && writePermission) { startVGuard(); } else { Toast.makeText(IntroActivity.this, "? 6.0??? ? ? ?? ?? . ? .", Toast.LENGTH_LONG).show(); finish(); } } else { Toast.makeText(IntroActivity.this, "? 6.0??? ? ? ?? ?? . ? .", Toast.LENGTH_LONG).show(); finish(); } } /** * ???????VGuard??( ?? ? ?? ) * * @date BSJ 13/05/06 */ private void startVGuard() { if (!Common.isPackageInstalled(this, Constants.VGUARD_PACKAGE_NAME)) { dialogInstallVGuard(); return; } ComponentName cn = new ComponentName(Constants.VGUARD_PACKAGE_NAME, Constants.VGUARD_PACKAGE_NAME + Constants.VGUARD_ACTIVITY_NAME); Intent actIntent = new Intent(Intent.ACTION_MAIN); actIntent.setComponent(cn); actIntent.addCategory(Intent.CATEGORY_LAUNCHER); actIntent.putExtra("LicenseKey", Constants.VGUARD_LICENSE_KEY); startActivityForResult(actIntent, REQUEST_START_VGUARD); CLog.e("VGuard", "start"); } @SuppressWarnings("unused") @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_START_VGUARD) { if (resultCode == RESULT_OK) { if ("Y".equals(data.getStringExtra("rooting_ck"))) { new IntroAppValidateRequest(Constants.URL_APPCHECK_ROOTING, new AppValidateCheckHandler(), getApplicationContext(), mE2EFlag); mErrorFlag = true; mIntent.putExtra("Rooting", true); mLoginCode = LOGIN_CODE_ERROR; CLog.d("CHECK_ROOTING", "ROOTING PHONE"); } else if ("Y".equals(data.getStringExtra("virus_ck"))) { CLog.d("CHECK_VIRUS", "VIRUS COUNT = [" + data.getStringExtra("virus_count") + "]"); } startIntroTimer(); if (kr.go.hf.hfappandroid.plugin.common.Constants.WIFI_FLAG && kr.go.hf.hfappandroid.plugin.common.Common.isWiFi(getApplicationContext())) { new AlertDialog.Builder(this).setTitle("") .setMessage(getString(R.string.error_network_status_wifi)) .setPositiveButton("?", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); mLoginCode = LOGIN_CODE_FAIL; finish(); } }).show(); } else { checkVersion(this); } } else { CLog.e(TAG, "VGuard Error resultCode : " + resultCode); CRSToolkit.addCRSCustomData("[IntroActivity.java]VGuard Error resultCode : " + resultCode); mLoginCode = LOGIN_CODE_FAIL; finish(); } } } /** * ??? */ @SuppressLint("NewApi") private final void startIntroTimer() { /* * ?????? * long ???????? ??? * ?????????3? ? ?? ? ??? * ????? ??? */ long longTime = Utils.getDateLong(); setContentView(R.layout.intro_activity); ImageView ivView = (ImageView) findViewById(R.id.intro); if ((longTime % 3) == 0) { ivView.setImageResource(R.drawable.intro_01); } else if ((longTime % 3) == 1) { ivView.setImageResource(R.drawable.intro_02); } else if ((longTime % 3) == 2) { ivView.setImageResource(R.drawable.intro_03); } mIntroTimer = new Timer(); final TimerTask introTimeTask = new TimerTask() { @Override public void run() { if (mLoginCode == LOGIN_CODE_DEFAULT) { return; } else if (mLoginCode == LOGIN_CODE_FAIL) { finish(); if (mIntroTimer != null) mIntroTimer.cancel(); this.cancel(); return; } else if (mLoginCode == LOGIN_CODE_ERROR) { return; } if (mErrorFlag) { mIntent.setClass(IntroActivity.this, ErrorActivity.class); } else { mIntent.setClass(IntroActivity.this, MainActivity.class); } startActivity(mIntent); finish(); if (mIntroTimer != null) mIntroTimer.cancel(); this.cancel(); } }; mIntroTimer.schedule(introTimeTask, 1500, 200); } /** * ?IP ? * * @date BSJ 13/05/06 */ private void checkGlobalIp() { new IntroAppValidateRequest(Constants.URL_APPCHECK_GLOBAL, new AppValidateCheckHandler() { @Override public void success(String code, String message) { startAppDefence(); } @Override public void nodeIsNull() { showAlertDialog(getString(R.string.dialog_network_error)); } @Override public void error(String code, String msg) { mErrorFlag = true; mIntent.putExtra("GlobalIp", true); mLoginCode = LOGIN_CODE_ERROR; CLog.d("CHECK_FOREIGN_IP", "FOREIGN_IP"); startAppDefence(); } }, getApplicationContext(), mE2EFlag, "registrationId", PluginPreference.getPushId(getApplicationContext())); } /** * ?????? * * @date BSJ 13/05/07 */ private void startAppDefence() { AppDefenceUI.OnAAPlusFinish callback = new AppDefenceUI.OnAAPlusFinish() { @Override public void onAAPlusFinish(boolean result, String strToken, String strClientValue, String strError) { CLog.d("CHECK_APP_DEFENCE", "FORGERY_APP"); if (result) { String strValue = "AAPLUS_TOKEN=" + strToken + "&AAPLUS_CLIENT_VALUE=" + strClientValue; initE2E(strValue); } else { if (strError.trim().indexOf(Constants.APP_DEFENCE_SEPARATOR) == -1) { CRSToolkit.addCRSCustomData( "[IntroActivity.java] ?????? ?: " + strError); showAlertDialog(getString(R.string.dialog_forgery)); return; } mErrorFlag = true; mIntent.putExtra("AppDefenceError", true); mLoginCode = LOGIN_CODE_ERROR; CLog.d("CHECK_APP_DEFENCE", "FORGERY_APP"); new IntroAppValidateRequest(Constants.URL_APPCHECK_FORGERY, new AppValidateCheckHandler(), getApplicationContext(), mE2EFlag); } checkRooting(); checkFirstExecute(); } }; AppDefenceUI.setFinishCallback(callback); // ?????? ?(Context, App name, progress massage) AppDefenceUI.StartAAPlus_Start(this, "HFAPPANDROID", null); } /** * ? */ private void checkRooting() { int nCheckRooting = CheckRooting.checkRooting(true, false); if (nCheckRooting == 1) { mErrorFlag = true; mIntent.putExtra("Rooting", true); mLoginCode = LOGIN_CODE_ERROR; CLog.d("CHECK_ROOTING", "ROOTING PHONE"); } else if (nCheckRooting == 2) { mErrorFlag = true; mIntent.putExtra("Rooting", true); mLoginCode = LOGIN_CODE_ERROR; CLog.d("CHECK_ROOTING", "TEGRAK PHONE"); } } /** * E2E ?? */ @SuppressLint("HandlerLeak") private void initE2E(String strValue) { E2E.getInstance().makeNewKey(new Handler() { @Override public void handleMessage(Message msg) { String message = msg.obj.toString(); CLog.e(TAG, "E2E make new key : " + message); if (!"success".equals(message)) { mE2EFlag = false; CLog.d("CHECK_E2E_INITIALIZE", "FAIL"); CRSToolkit.addCRSCustomData("[IntroActivity.java] E2E INITIALIZE FAIL"); new IntroAppValidateRequest(Constants.URL_APPCHECK_E2E, new AppValidateCheckHandler(), getApplicationContext(), mE2EFlag, "registrationId", PluginPreference.getPushId(getApplicationContext()), "message", message); /* TODO: E2E ??? ?? */ } else { mE2EFlag = true; } super.handleMessage(msg); } }, strValue); } /** * ???? ? */ private void checkFirstExecute() { if (PreferenceUtils.isFirstExecute(getApplicationContext())) { CLog.e(TAG, "first execute"); new IntroAppValidateRequest(Constants.URL_APPCHECK_DOWNLOAD, new AppValidateCheckHandler() { @Override public void success(String code, String message) { PreferenceUtils.setFirstFlag(getApplicationContext(), false); super.success(code, message); } @Override public void registrationSuccess(String configId) { PluginPreference.setConfigId(getApplicationContext(), configId); initPush(); super.registrationSuccess(configId); } }, getApplicationContext(), false, "registrationId", Constants.INIT_REGISTRATION_ID); } else if (kr.go.hf.hfappandroid.plugin.common.Common .isNullString(PluginPreference.getPushId(getApplicationContext()))) { if (kr.go.hf.hfappandroid.plugin.common.Common .isNullString(PluginPreference.getConfigId(getApplicationContext()))) { new IntroAppValidateRequest(Constants.URL_APPCHECK_DOWNLOAD, new AppValidateCheckHandler() { @Override public void success(String code, String message) { PreferenceUtils.setFirstFlag(getApplicationContext(), false); super.success(code, message); } @Override public void registrationSuccess(String configId) { PluginPreference.setConfigId(getApplicationContext(), configId); initPush(); super.registrationSuccess(configId); } }, getApplicationContext(), false, "registrationId", Constants.INIT_REGISTRATION_ID); } else { initPush(); } } mLoginCode = LOGIN_CODE_SUCCESS; } /** * push id ? */ private void initPush() { Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); Parcelable value = PendingIntent.getBroadcast(this, 0, new Intent(), 0); registrationIntent.setPackage("com.google.android"); registrationIntent.putExtra("app", value); registrationIntent.putExtra("sender", Constants.PROJECT_NUMBER); startService(registrationIntent); } /** * ? */ private void checkVersion(final Context context) { HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("devicePlatForm", Constants.DEVICE_INFO_PLATFORM); HttpCommon.request(Constants.URL_APPCHECK_VERSION, parameters, new AppValidateCheckHandler() { @Override public void success(String code, String message) { super.success(code, message); if (Integer.parseInt(message.replace(".", "")) <= Integer .parseInt(getString(R.string.app_version).replace(".", ""))) { checkGlobalIp(); } else { new AlertDialog.Builder(context).setTitle("").setMessage(getString(R.string.dialog_app_update)) .setPositiveButton("?", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://search?q=" + Constants.APP_PAGAGE_NAME)); startActivity(intent); dialog.dismiss(); mLoginCode = LOGIN_CODE_FAIL; finish(); } }).show(); } } @Override public void error(String code, String msg) { showAlertDialog(getString(R.string.dialog_network_error)); super.error(code, msg); } @Override public void nodeIsNull() { showAlertDialog(getString(R.string.dialog_network_error)); super.nodeIsNull(); } }, getApplicationContext()); } @Override public void onBackPressed() { if (mIntroTimer != null) mIntroTimer.cancel(); super.onBackPressed(); } private void dialogInstallVGuard() { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setMessage(getString(R.string.dialog_vguard)).setCancelable(false) .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://search?q=" + Constants.VGUARD_PACKAGE_NAME)); startActivity(intent); finish(); } }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mLoginCode = LOGIN_CODE_FAIL; finish(); } }).show(); } private void showAlertDialog(String message) { new AlertDialog.Builder(this).setMessage(message) .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mLoginCode = LOGIN_CODE_FAIL; dialog.dismiss(); } }).show(); } private void showAlertDialogThenExit(String message) { new AlertDialog.Builder(this).setMessage(message) .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mLoginCode = LOGIN_CODE_FAIL; dialog.dismiss(); finish(); } }).show(); } @Override protected void onDestroy() { if (mLoginCode == LOGIN_CODE_FAIL) { Intent realscan_intent = new Intent(Constants.VGUARD_SERVICE_NAME); if (realscan_intent != null) { realscan_intent.putExtra("VGUARD", "STOP_RUNAPP_SCAN"); realscan_intent.putExtra("PKG", "kr.go.hf.hfappandroid"); if (realscan_intent != null) { realscan_intent.setPackage("kr.co.ittech.bank"); startService(realscan_intent); } } CLog.e(TAG, "c finish"); } super.onDestroy(); } }