Added move dialog on start and list item buttons (move/delete).

This commit is contained in:
Felix Ableitner 2014-07-22 01:53:42 +02:00
parent 90a1973853
commit f7952aa9ce
6 changed files with 87 additions and 15 deletions

View file

@ -13,6 +13,13 @@ android {
testHandleProfiling true testHandleProfiling true
testFunctionalTest true testFunctionalTest true
} }
buildTypes {
debug {
applicationIdSuffix ".debug"
debuggable true
}
}
} }
dependencies { dependencies {

View file

@ -5,6 +5,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
/** /**
@ -12,8 +13,15 @@ import android.widget.TextView;
*/ */
public class CertificateAdapter extends ArrayAdapter<Certificate> { public class CertificateAdapter extends ArrayAdapter<Certificate> {
public CertificateAdapter(Context context) { private CertificateManager mCertificateManager;
super(context, android.R.layout.simple_list_item_1);
private MovedCertificatesStorage mMovedCertificatesStorage;
public CertificateAdapter(Context context, CertificateManager certificateManager,
MovedCertificatesStorage movedCertificatesStorage) {
super(context, 0);
mCertificateManager = certificateManager;
mMovedCertificatesStorage = movedCertificatesStorage;
} }
@Override @Override
@ -21,15 +29,37 @@ public class CertificateAdapter extends ArrayAdapter<Certificate> {
if (convertView == null) { if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext() LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); convertView = inflater.inflate(R.layout.certificate_list_item, parent, false);
} }
TextView title = (TextView) convertView.findViewById(android.R.id.text1); final Certificate cert = getItem(position);
title.setText(getItem(position).getFile().getName()); TextView title = (TextView) convertView.findViewById(R.id.title);
convertView.setBackgroundColor(getContext().getResources().getColor( title.setText(cert.getFile().getName());
(getItem(position).isSystemCertificate()) Button button = (Button) convertView.findViewById(R.id.button);
? R.color.background_system_certificate int colorRes;
: R.color.background_user_certificate)); if (cert.isSystemCertificate()) {
button.setText(R.string.delete);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCertificateManager.deleteCertificate(cert);
mMovedCertificatesStorage.delete(cert);
}
});
colorRes = R.color.background_system_certificate;
}
else {
button.setText(R.string.move_to_system);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCertificateManager.moveCertificateToSystem(cert);
mMovedCertificatesStorage.insert(cert);
}
});
colorRes = R.color.background_user_certificate;
}
convertView.setBackgroundColor(getContext().getResources().getColor(colorRes));
return convertView; return convertView;
} }

View file

@ -8,7 +8,6 @@ import android.widget.ListView;
import java.util.List; import java.util.List;
public class MainActivity extends ListActivity { public class MainActivity extends ListActivity {
private ListView mListView; private ListView mListView;
@ -27,15 +26,21 @@ public class MainActivity extends ListActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mListView = getListView(); mListView = getListView();
mCertificateAdapter = new CertificateAdapter(this);
mCertificateManager = new CertificateManager(); mCertificateManager = new CertificateManager();
mMovedCertificatesStorage = new MovedCertificatesStorage(this); mMovedCertificatesStorage = new MovedCertificatesStorage(this);
mCertificateAdapter =
new CertificateAdapter(this, mCertificateManager, mMovedCertificatesStorage);
mCertificateAdapter.addAll(mCertificateManager.getCertificates(false)); mCertificateAdapter.addAll(mCertificateManager.getCertificates(false));
// FIXME: nothing listed
// -> db table is empty
mCertificateAdapter.addAll(mMovedCertificatesStorage.list()); mCertificateAdapter.addAll(mMovedCertificatesStorage.list());
mListView.setAdapter(mCertificateAdapter); mListView.setAdapter(mCertificateAdapter);
} }
/**
* Shows a dialog to move all user certificates to system storage.
*/
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@ -48,7 +53,7 @@ public class MainActivity extends ListActivity {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
for (Certificate c : list) { for (Certificate c : list) {
mCertificateManager.moveCertificateToSystem(c); c = mCertificateManager.moveCertificateToSystem(c);
mMovedCertificatesStorage.insert(c); mMovedCertificatesStorage.insert(c);
} }
} }

View file

@ -9,6 +9,7 @@ import android.provider.BaseColumns;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -32,8 +33,7 @@ public class MovedCertificatesStorage implements Closeable {
} }
public boolean insert(Certificate cert) { public boolean insert(Certificate cert) {
if (!cert.isSystemCertificate()) assert(cert.isSystemCertificate());
return false;
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();
cv.put(Table.COLUMN_NAME_FILE_NAME, cert.getFile().getName()); cv.put(Table.COLUMN_NAME_FILE_NAME, cert.getFile().getName());

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dip"
android:textAppearance="?android:attr/textAppearanceListItem"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/title"
android:layout_alignParentRight="true"
style="?android:attr/buttonStyleSmall"/>
</RelativeLayout>

View file

@ -7,4 +7,8 @@
<string name="dialog_move_certs_message">Should they be moved to system storage?</string> <string name="dialog_move_certs_message">Should they be moved to system storage?</string>
<string name="move_to_system">Move to System</string>
<string name="delete">Delete</string>
</resources> </resources>