一、前言
当用户使用剪切功能时,可能用于剪切密码等敏感信息,导致信息泄露。下面分别从删除复制/剪切菜单、删除长按菜单常用操作进行代码示例说明
二、代码说明
1.删除复制/剪切菜单 - 通过删除复制和剪切选项,避免用户进行敏感数据的复制和剪切。安全要点及编码示例:
1、删除复制选项。@b@2、删除剪切选项。
UncopyableActivity.java@b@ @b@package org.jssec.android.clipboard.leakage;@b@ @b@import android.app.Activity;@b@import android.os.Bundle;@b@import android.support.v4.app.NavUtils;@b@import android.view.ActionMode;@b@import android.view.Menu;@b@import android.view.MenuItem;@b@import android.widget.EditText;@b@ @b@public class UncopyableActivity extends Activity {@b@ private EditText copyableEdit;@b@ private EditText uncopyableEdit;@b@ @b@ @Override@b@ public void onCreate(Bundle savedInstanceState) {@b@ super.onCreate(savedInstanceState);@b@ setContentView(R.layout.uncopyable);@b@ @b@ copyableEdit = (EditText) findViewById(R.id.copyable_edit);@b@ uncopyableEdit = (EditText) findViewById(R.id.uncopyable_edit);@b@ // By setCustomSelectionActionMODECallback method,@b@ // Possible to customize menu of character string selection.@b@ uncopyableEdit.setCustomSelectionActionModeCallback(actionModeCallback);@b@ }@b@ @b@ private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {@b@ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {@b@ return false;@b@ }@b@ @b@ public void onDestroyActionMode(ActionMode mode) {@b@ }@b@ @b@ public boolean onCreateActionMode(ActionMode mode, Menu menu) {@b@ // *** POINT 1 *** Delete android.R.id.copy from the menu of character string selection.@b@ MenuItem itemCopy = menu.findItem(android.R.id.copy);@b@ if (itemCopy != null) {@b@ menu.removeItem(android.R.id.copy);@b@ }@b@ // *** POINT 2 *** Delete android.R.id.cut from the menu of character string selection.@b@ MenuItem itemCut = menu.findItem(android.R.id.cut);@b@ if (itemCut != null) {@b@ menu.removeItem(android.R.id.cut);@b@ }@b@ return true;@b@ }@b@ @b@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {@b@ return false;@b@ }@b@ };@b@ @b@ @Override@b@ public boolean onCreateOptionsMenu(Menu menu) {@b@ getMenuInflater().inflate(R.menu.uncopyable, menu);@b@ return true;@b@ }@b@ @b@ @Override@b@ public boolean onOptionsItemSelected(MenuItem item) {@b@ switch (item.getItemId()) {@b@ case android.R.id.home:@b@ NavUtils.navigateUpFromSameTask(this);@b@ return true;@b@ }@b@ return super.onOptionsItemSelected(item);@b@ }@b@}
2.删除长按菜单 - 可以通过修改layout xml中的android:longClickable为false。具体方法如下:
unlongclickable.xml@b@<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"android:orientation="vertical">@b@ <TextView@b@ android:layout_width="match_parent"@b@ android:layout_height="wrap_content" android:text="@string/unlongclickable_description" />@b@ @b@ <!-- EditText to prohibit copy/cut EditText -->@b@ <!-- *** POINT 1 *** Set false to android:longClickable in View to prohibit copy/cut. -->@b@ <EditText@b@ android:layout_width="match_parent"@b@ android:layout_height="wrap_content"@b@ android:longClickable="false"@b@ android:hint="@string/unlongclickable_hint" />@b@</LinearLayout>