diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 42deefbd..c22c6464 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -1,7 +1,9 @@ package protect.card_locker; +import android.content.ClipData; import android.content.DialogInterface; import android.content.Intent; +import android.content.ClipboardManager; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.database.Cursor; @@ -10,13 +12,16 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; +import android.view.ContextMenu; import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.webkit.WebView; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; +import android.widget.Toast; import java.util.Calendar; @@ -65,6 +70,8 @@ private void updateLoyaltyCardList() final LoyaltyCardCursorAdapter adapter = new LoyaltyCardCursorAdapter(this, cardCursor); cardList.setAdapter(adapter); + registerForContextMenu(cardList); + cardList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override @@ -83,6 +90,39 @@ public void onItemClick(AdapterView> parent, View view, int position, long id) }); } + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) + { + super.onCreateContextMenu(menu, v, menuInfo); + if (v.getId()==R.id.list) + { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.card_longclick_menu, menu); + } + } + + @Override + public boolean onContextItemSelected(MenuItem item) + { + AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); + ListView listView = (ListView) findViewById(R.id.list); + + Cursor cardCursor = (Cursor)listView.getItemAtPosition(info.position); + LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor); + + if(card != null && item.getItemId() == R.id.action_clipboard) + { + ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText(card.store, card.cardId); + clipboard.setPrimaryClip(clip); + + Toast.makeText(this, R.string.copy_to_clipboard_toast, Toast.LENGTH_LONG).show(); + return true; + } + + return super.onContextItemSelected(item); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { diff --git a/app/src/main/res/menu/card_longclick_menu.xml b/app/src/main/res/menu/card_longclick_menu.xml new file mode 100644 index 00000000..67ac03c8 --- /dev/null +++ b/app/src/main/res/menu/card_longclick_menu.xml @@ -0,0 +1,9 @@ + +
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b4eb38ac..bf51b7b3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,6 +18,7 @@