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
|
public class MainActivity extends Activity
{
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//On essaie d'ouvrir un lecteur de QRcode, si oui : on lit; si non : on en télécharge un
public void scanQR(View v)
{
try
{
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
catch (ActivityNotFoundException anfe)
{
showDialog(MainActivity.this, "Aucun scanner trouvé.", "En télécharger un ?", "Oui", "Non").show();
}
}
//On demande à l'utilisateur d'installer un lecteur de QRcode
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo)
{
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialogInterface, int i)
{
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
}
catch (ActivityNotFoundException ignored)
{
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i)
{
}
});
return downloadDialog.show();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Telechargement en cours...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
setContentView(R.layout.activity_download);
DownloadTask mDownload = new DownloadTask();
mDownload.execute(contents);
}
}
}
//On télécharge le certificat, on l'installe (ce qui le déplace dans le keystore) puis, si il reste des traces, on le supprime
class DownloadTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String ...f_url) {
try {
Context context = getApplicationContext();
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
String fileName = url.getFile().substring(url.getFile().lastIndexOf('/') + 1);
File file = new File(context.getFilesDir().getAbsolutePath() + "/" + fileName);
if (file.exists()) {
file.delete();
}
InputStream input = new BufferedInputStream(url.openStream());
byte[] buffer = new byte[1024];
long total = 0;
FileOutputStream output = context.openFileOutput(fileName, Context.MODE_PRIVATE);
int count;
while ((count = input.read(buffer)) != -1)
{
total += count;
publishProgress("" + (int)((total * 100) / fileLength));
output.write(buffer, 0, count);
}
output.flush();
output.close();
if (file.exists()) {
Intent intent = KeyChain.createInstallIntent();
file = new File(context.getFilesDir().getAbsolutePath() + "/" + fileName);
byte[] p12 = FileUtils.readFileToByteArray(file);
intent.putExtra(KeyChain.EXTRA_PKCS12, p12);
startActivity(intent);
file = new File(context.getFilesDir().getAbsolutePath() + "/" + fileName);
if (file.exists()) {
file.delete();
}
}
else
return null;
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return (null);
}
@Override
protected void onProgressUpdate(String... values) {
pDialog.setProgress(Integer.parseInt(values[0]));
}
@Override
protected void onPostExecute(String fileName) {
try {
wait(100);
}
catch (Exception e) {
Log.e("Error;", e.getMessage());
}
dismissDialog(progress_bar_type);
TextView text = (TextView)findViewById(R.id.textView);
text.setText("Téléchargement terminé, installation en cours");
}
}
} |