2016-08-15 21:29:41 +02:00
|
|
|
package me.impy.aegis;
|
|
|
|
|
2016-08-21 22:32:07 +02:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
2016-08-21 22:24:04 +02:00
|
|
|
import android.content.Context;
|
2016-08-15 22:31:28 +02:00
|
|
|
import android.content.Intent;
|
2016-09-29 12:31:55 +02:00
|
|
|
import android.content.SharedPreferences;
|
2016-08-16 14:14:17 +02:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2016-08-15 21:29:41 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
2016-08-16 20:04:38 +02:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2016-08-16 14:14:17 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2016-08-21 22:54:27 +02:00
|
|
|
import android.support.v7.widget.helper.ItemTouchHelper;
|
2016-08-16 14:14:17 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2016-08-15 22:31:28 +02:00
|
|
|
import android.view.View;
|
2016-08-17 01:14:25 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.yarolegovich.lovelydialog.LovelyTextInputDialog;
|
2016-08-16 00:08:01 +02:00
|
|
|
|
2016-08-16 20:04:38 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2016-08-21 23:13:27 +02:00
|
|
|
import github.nisrulz.recyclerviewhelper.RVHItemClickListener;
|
2016-08-21 22:54:27 +02:00
|
|
|
import github.nisrulz.recyclerviewhelper.RVHItemDividerDecoration;
|
|
|
|
import github.nisrulz.recyclerviewhelper.RVHItemTouchHelperCallback;
|
2016-08-22 19:31:03 +02:00
|
|
|
import me.impy.aegis.crypto.CryptoUtils;
|
2016-08-16 13:31:22 +02:00
|
|
|
import me.impy.aegis.crypto.OTP;
|
2016-08-22 19:31:03 +02:00
|
|
|
import me.impy.aegis.db.Database;
|
2016-08-15 21:29:41 +02:00
|
|
|
|
2016-08-21 22:24:04 +02:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
2016-08-15 21:29:41 +02:00
|
|
|
|
2016-08-16 00:08:01 +02:00
|
|
|
static final int GET_KEYINFO = 1;
|
2016-08-24 23:48:25 +02:00
|
|
|
static final int ADD_KEYINFO = 2;
|
|
|
|
|
2016-08-16 20:04:38 +02:00
|
|
|
RecyclerView rvKeyProfiles;
|
|
|
|
KeyProfileAdapter mKeyProfileAdapter;
|
|
|
|
ArrayList<KeyProfile> mKeyProfiles;
|
2016-08-22 19:31:03 +02:00
|
|
|
Database database;
|
2016-08-16 20:54:53 +02:00
|
|
|
|
|
|
|
int count = 0;
|
2016-08-16 14:14:17 +02:00
|
|
|
|
2016-08-15 21:29:41 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2016-09-29 12:31:55 +02:00
|
|
|
|
|
|
|
SharedPreferences prefs = this.getSharedPreferences("me.impy.aegis", Context.MODE_PRIVATE);
|
|
|
|
if(!prefs.getBoolean("passedIntro", false))
|
|
|
|
{
|
|
|
|
Intent intro = new Intent(this, IntroActivity.class);
|
|
|
|
startActivity(intro);
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:29:41 +02:00
|
|
|
setContentView(R.layout.activity_main);
|
2016-08-16 14:14:17 +02:00
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
2016-08-15 22:31:28 +02:00
|
|
|
|
2016-08-16 14:14:17 +02:00
|
|
|
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
|
|
|
fab.setOnClickListener(new View.OnClickListener() {
|
2016-08-15 22:31:28 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
Intent scannerActivity = new Intent(getApplicationContext(), ScannerActivity.class);
|
2016-08-16 00:08:01 +02:00
|
|
|
startActivityForResult(scannerActivity, GET_KEYINFO);
|
2016-08-15 22:31:28 +02:00
|
|
|
}
|
|
|
|
});
|
2016-08-16 14:14:17 +02:00
|
|
|
|
2016-08-22 19:31:03 +02:00
|
|
|
// demo
|
|
|
|
char[] password = "test".toCharArray();
|
|
|
|
database = Database.createInstance(getApplicationContext(), "keys.db", password);
|
|
|
|
CryptoUtils.zero(password);
|
|
|
|
|
2016-08-16 20:04:38 +02:00
|
|
|
mKeyProfiles = new ArrayList<>();
|
|
|
|
|
|
|
|
rvKeyProfiles = (RecyclerView) findViewById(R.id.rvKeyProfiles);
|
|
|
|
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
|
|
|
|
rvKeyProfiles.setLayoutManager(mLayoutManager);
|
|
|
|
|
2016-08-21 22:24:04 +02:00
|
|
|
final Context context = this.getApplicationContext();
|
2016-08-21 23:13:27 +02:00
|
|
|
rvKeyProfiles.addOnItemTouchListener(new RVHItemClickListener(this, new RVHItemClickListener.OnItemClickListener() {
|
2016-08-21 22:24:04 +02:00
|
|
|
@Override
|
2016-08-21 23:13:27 +02:00
|
|
|
public void onItemClick(View view, int position) {
|
2016-08-21 22:32:07 +02:00
|
|
|
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
2016-08-21 23:13:27 +02:00
|
|
|
ClipData clip = ClipData.newPlainText("text/plain", mKeyProfiles.get(position).Code);
|
2016-08-21 22:32:07 +02:00
|
|
|
clipboard.setPrimaryClip(clip);
|
|
|
|
|
|
|
|
Toast.makeText(context, "Code successfully copied to the clipboard", Toast.LENGTH_SHORT).show();
|
2016-08-21 22:24:04 +02:00
|
|
|
}
|
2016-08-21 23:13:27 +02:00
|
|
|
}));
|
2016-08-21 22:24:04 +02:00
|
|
|
|
2016-08-21 23:13:27 +02:00
|
|
|
mKeyProfileAdapter = new KeyProfileAdapter(mKeyProfiles);
|
2016-08-21 22:54:27 +02:00
|
|
|
rvKeyProfiles.addItemDecoration(new RVHItemDividerDecoration(this, LinearLayoutManager.VERTICAL));
|
|
|
|
|
|
|
|
ItemTouchHelper.Callback callback = new RVHItemTouchHelperCallback(mKeyProfileAdapter, true, false, false);
|
|
|
|
ItemTouchHelper helper = new ItemTouchHelper(callback);
|
|
|
|
helper.attachToRecyclerView(rvKeyProfiles);
|
|
|
|
|
2016-08-16 20:04:38 +02:00
|
|
|
rvKeyProfiles.setAdapter(mKeyProfileAdapter);
|
2016-08-22 19:31:03 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
for (KeyProfile profile : database.getKeys()) {
|
|
|
|
mKeyProfiles.add(profile);
|
|
|
|
}
|
|
|
|
mKeyProfileAdapter.notifyDataSetChanged();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-08-15 21:29:41 +02:00
|
|
|
}
|
2016-08-16 00:08:01 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
// Check which request we're responding to
|
|
|
|
if (requestCode == GET_KEYINFO) {
|
|
|
|
// Make sure the request was successful
|
|
|
|
if (resultCode == RESULT_OK) {
|
2016-08-17 01:14:25 +02:00
|
|
|
final KeyProfile keyProfile = (KeyProfile)data.getSerializableExtra("KeyProfile");
|
2016-08-16 00:08:01 +02:00
|
|
|
|
2016-08-24 01:26:33 +02:00
|
|
|
Intent intent = new Intent(this, AddProfileActivity.class);
|
2016-08-24 23:48:25 +02:00
|
|
|
intent.putExtra("KeyProfile", keyProfile);
|
|
|
|
startActivityForResult(intent, ADD_KEYINFO);
|
2016-08-16 20:04:38 +02:00
|
|
|
//TODO: do something with the result.
|
2016-08-16 00:08:01 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-24 23:48:25 +02:00
|
|
|
else if (requestCode == ADD_KEYINFO) {
|
|
|
|
// Make sure the request was successful
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
final KeyProfile keyProfile = (KeyProfile)data.getSerializableExtra("KeyProfile");
|
|
|
|
|
|
|
|
String otp;
|
|
|
|
try {
|
|
|
|
otp = OTP.generateOTP(keyProfile.Info);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyProfile.Code = otp;
|
|
|
|
mKeyProfiles.add(keyProfile);
|
|
|
|
mKeyProfileAdapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
try {
|
|
|
|
database.addKey(keyProfile);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 00:08:01 +02:00
|
|
|
}
|
2016-08-16 14:14:17 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
// Handle action bar item clicks here. The action bar will
|
|
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
|
int id = item.getItemId();
|
|
|
|
|
|
|
|
//noinspection SimplifiableIfStatement
|
|
|
|
if (id == R.id.action_settings) {
|
2016-09-30 01:07:56 +02:00
|
|
|
|
|
|
|
Intent preferencesActivity = new Intent(this, PreferencesActivity.class);
|
|
|
|
startActivity(preferencesActivity);
|
|
|
|
|
2016-08-16 14:14:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2016-08-15 21:29:41 +02:00
|
|
|
}
|