Implement the EasyTouch control of IOS-like AssistiveTouch

Source: Internet
Author: User

Implement the EasyTouch control of IOS-like AssistiveTouch
Overview:

I have heard a piece of news before, that is to say, the intent of the ivetouch in Ipone is intended for the disabled. This feature is most frequently used in Asia (China.

Although I do not know the reliability of this news, what is no surprise is that it does bring great convenience to our mobile phone operations. Before this design, it may be easier to think of creating a shortcut, while the operation result of the shortcut is still to load the interface (sometimes it may be a heavy interface ). Once this path is taken, the distance from the shortcut operation may be farther and farther away.

The Design of AssistiveTouch is indeed awesome. Android is also worth having this feature. Next I will briefly explain how to implement this feature on Android.

 

Train of Thought:

At a glance, we may be confused about how to add controls above the system desktop in Android. Yes, this is a difficult point. In terms of size, you may have thought of the Dialog, but the Dialog in Android cannot be displayed on the system desktop. Then you may say that it is not a pattern for the Dialog topic of the Activity? Yes, it does solve the problem of the pop-up window above the system desktop. However, we need to drag and drop controls at will, which may not be easy for Android.

However, in Android, you can add a View to WindowManager. The window mechanism in Android is implemented based on WindowManager. WindowManager adds a View to the screen or removes the View from the screen. It is the bottom layer of the display View.

Well, it does. WindowManger is the key to implementation. Let's implement it.

Note that EasyTouchView is created based on a common Context. If EasyTouchView is created based on a Context with a short life cycle like Activity, easyTouchView will soon disappear with the suspension or destruction of the Activity.

 

Implementation process:

EasyTouchView:

Package com. bumblebee. remindeasy. widgets; import java. util. timer; import java. util. timerTask; import com. bumblebee. remindeasy. r; import android. content. context; import android. graphics. color; import android. graphics. drawable. bitmapDrawable; import android. OS. handler; import android. OS. message; import android. view. gravity; import android. view. layoutInflater; import android. view. motionEvent; import android. view. View; import android. view. windowManager; import android. view. windowManager. layoutParams; import android. widget. button; import android. widget. imageView; import android. widget. popupWindow; import android. widget. toast; public class EasyTouchView extends View {private Context mContext; private WindowManager mWManager; private WindowManager. layoutParams mWMParams; private View mTouchView; private ImageView m IconImageView = null; private PopupWindow mPopuWin; private ServiceListener mSerLisrener; private View mSettingTable; private int mTag = 0; private int midX; private int midY; private int mOldOffsetX; private int mOldOffsetY; private Toast mToast; private Timer mTimer = null; private TimerTask mTask = null; public EasyTouchView (Context context, ServiceListener listener) {super (context); mContext = Context; mSerLisrener = listener;} public void initTouchViewEvent () {initEasyTouchViewEvent (); initSettingTableView ();} private void initEasyTouchViewEvent () {// set the parameter mWManager = (WindowManager) mContext for loading view WindowManager. getSystemService (Context. WINDOW_SERVICE); midX = mWManager. getdefadisplay display (). getWidth ()/2-25; midY = mWManager. getdefadisplay display (). getHeight ()/2-44; mTouchView = LayoutInflater. from (mContext ). inflate (R. layout. easy_touch_view, null); mIconImageView = (ImageView) mTouchView. findViewById (R. id. easy_touch_view_imageview); mTouchView. setBackgroundColor (Color. TRANSPARENT); mTouchView. setOnTouchListener (mTouchListener); WindowManager wm = mWManager; WindowManager. layoutParams wmParams = new WindowManager. layoutParams (); mWMParams = wmParams; wmParams. type = 2003; // Here 2002 indicates the system-level window. You can also try 2003. WmParams. flags = 40; // set wmParams to be controllable on the desktop. width = 100; wmParams. height = 100; wmParams. format =-3; // transparent wm. addView (mTouchView, wmParams);} private void initSettingTableView () {mSettingTable = LayoutInflater. from (mContext ). inflate (R. layout. show_setting_table, null); Button commonUseButton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_common_use_button); Button screenLockButto N = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_screen_lock_button); Button icationicationbutton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_icationication_button); Button phoneButton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_phone_button); Button pageButton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_page_butt On); Button cameraButton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_camera_button); Button backButton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_back_button); Button homeButton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_home_button); Button exitTouchButton = (Button) mSettingTable. findViewById (R. id. show_setting_table_item_exit _ Touch_button); commonUseButton. setOnClickListener (mClickListener); screenLockButton. setOnClickListener (mClickListener); icationicationbutton. setOnClickListener (mClickListener); phoneButton. setOnClickListener (mClickListener); pageButton. setOnClickListener (mClickListener); cameraButton. setOnClickListener (mClickListener); backButton. setOnClickListener (mClickListener); homeButton. setOnClickListener (mCl IckListener); exitTouchButton. setOnClickListener (mClickListener);} private OnClickListener mClickListener = new OnClickListener () {@ Override public void onClick (View v) {switch (v. getId () {case R. id. show_setting_table_item_common_use_button: hideSettingTable (commonly used); break; case R. id. show_setting_table_item_screen_lock_button: hideSettingTable (lock screen); break; case R. id. show_setting_table_item_ic IC Ation_button: hideSettingTable (notification); break; case R. id. show_setting_table_item_phone_button: hideSettingTable (phone number); break; case R. id. show_setting_table_item_page_button: hideSettingTable (1); break; case R. id. show_setting_table_item_camera_button: hideSettingTable (CAMERA); break; case R. id. show_setting_table_item_back_button: hideSettingTable (returned); break; case R. id. show_setting_table_item_home_button: hide SettingTable (home page); break; case R. id. show_setting_table_item_exit_touch_button: quitTouchView (); break ;}}; private void quitTouchView () {hideSettingTable (exit); mWManager. removeView (mTouchView); mSerLisrener. onCloseService (true); clearTimerThead ();} private OnTouchListener mTouchListener = new OnTouchListener () {float lastX, lastY; int paramX, paramY; public boolean onTouch (View v, MotionEvent Event) {final int action = event. getAction (); float x = event. getRawX (); float y = event. getRawY (); if (mTag = 0) {mOldOffsetX = mWMParams. x; // offset: mOldOffsetY = mWMParams. y; // offset} switch (action) {case MotionEvent. ACTION_DOWN: motionActionDownEvent (x, y); break; case MotionEvent. ACTION_MOVE: motionActionMoveEvent (x, y); break; case MotionEvent. ACTION_UP: motionActionUpEvent (x, y); break; Default: break;} return true;} private void motionActionDownEvent (float x, float y) {lastX = x; lastY = y; paramX = mWMParams. x; paramY = mWMParams. y;} private void motionActionMoveEvent (float x, float y) {int dx = (int) (x-lastX); int dy = (int) (y-lastY); mWMParams. x = paramX + dx; mWMParams. y = paramY + dy; mTag = 1; // update the position of the floating window mWManager. updateViewLayout (mTouchView, mWMParams);} priva Te void motionActionUpEvent (float x, float y) {int newOffsetX = mWMParams. x; int newOffsetY = mWMParams. y; if (mOldOffsetX = newOffsetX & mOldOffsetY = newOffsetY) {mPopuWin = new PopupWindow (mSettingTable, LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); mPopuWin. setTouchInterceptor (new OnTouchListener () {public boolean onTouch (View v, MotionEvent event) {if (event. getAction () = Mot IonEvent. ACTION_OUTSIDE) {hideSettingTable (); return true ;}return false ;}); mPopuWin. setBackgroundDrawable (new BitmapDrawable (); mPopuWin. setTouchable (true); mPopuWin. setFocusable (true); mPopuWin. setOutsideTouchable (true); mPopuWin. setContentView (mSettingTable); if (Math. abs (mOldOffsetX)> midX) {if (mOldOffsetX> 0) {mOldOffsetX = midX;} else {mOldOffsetX =-midX;} if (Math. abs (mOld OffsetY)> midY) {if (mOldOffsetY> 0) {mOldOffsetY = midY;} else {mOldOffsetY =-midY;} mPopuWin. setAnimationStyle (R. style. animationPreview); mPopuWin. setFocusable (true); mPopuWin. update (); mPopuWin. showAtLocation (mTouchView, Gravity. CENTER,-mOldOffsetX,-mOldOffsetY); if (mTimer = null) {catchSettingTableDismiss () ;}} else {mTag = 0 ;}}; private void catchSettingTableDismiss () {m Timer = new Timer (); mTask = new TimerTask () {@ Override public void run () {if (mPopuWin = null |! MPopuWin. isShowing () {handler. sendEmptyMessage (0x0);} else {handler. sendEmptyMessage (0x1) ;}}}; mTimer. schedule (mTask, 0,100);} private void clearTimerThead () {if (mTask! = Null) {mTask. cancel (); mTask = null;} if (mTimer! = Null) {mTimer. cancel (); mTimer = null ;}} Handler handler = new Handler () {public void handleMessage (Message msg) {if (msg. what = 0x0) {mIconImageView. setBackgroundDrawable (getResources (). getDrawable (R. drawable. touch_ic);} else if (msg. what = 0x1) {mIconImageView. setBackgroundDrawable (getResources (). getDrawable (R. drawable. transparent) ;};}; public void showToast (Context context, Str Ing text) {if (mToast = null) {mToast = Toast. makeText (context, text, Toast. LENGTH_SHORT);} else {mToast. setText (text); mToast. setDuration (Toast. LENGTH_SHORT);} mToast. show ();} private void hideSettingTable (String content) {hideSettingTable (); showToast (mContext, content);} private void hideSettingTable () {if (null! = MPopuWin) {mPopuWin. dismiss () ;}} public interface ServiceListener {public void OnCloseService (boolean isClose );}}

AuxiliaryService:

 

 

public class AuxiliaryService extends Service implements ServiceListener {    private Intent mIntent;    @Override    public IBinder onBind(Intent intent) {        return null;    }    public void onCreate() {        super.onCreate();        new EasyTouchView(this, this).initTouchViewEvent();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        mIntent = intent;        return super.onStartCommand(intent, flags, startId);    }    @Override    public void OnCloseService(boolean isClose) {        stopService(mIntent);    }}

 

Note the following. We can see from the code above that we started EasyTouchView through Service. EasyTouch provides a screen lock function. To use one-click lock screen, you need to activate the Device Manager and jump to some of the system's interfaces. the startup of these interfaces cannot be Service-based and must be processed based on Activity. The Service-based startup process disappears after a flash.

Here we can start an Activity of our own in Service, and then start the interface for setting the Device Manager in this Activity.

 

The Code is as follows:

 

Public class AuxiliaryActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); lockScreen ();} private void lockScreen () {DevicePolicyManager mDevicePolicyManager; ComponentName mComponentName; mDevicePolicyManager = (DevicePolicyManager) getSystemService (Context. DEVICE_POLICY_SERVICE); mComponentName = new ComponentName (this, LockReceiver. class); // determine whether you have the permission if (mDevicePolicyManager. isAdminActive (mComponentName) {mDevicePolicyManager. lockNow (); finish () ;}else {activeManager (mComponentName) ;}/ *** activate the Device Manager to obtain permissions */private void activeManager (ComponentName componentName) {Intent intent = new Intent (DevicePolicyManager. ACTION_ADD_DEVICE_ADMIN); intent. putExtra (DevicePolicyManager. EXTRA_DEVICE_ADMIN, componentName); intent. putExtra (DevicePolicyManager. EXTRA_ADD_EXPLANATION, One key lock the screen); startActivity (intent); finish ();}}

 

 

:

 

TouchView

 

ShowTableView

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.