Bonjour,

Je développe une application de sécurité mobile dans le cadre d'un stage.
J'essaye actuellement de créer un service qui lit toutes les entrées TouchEvent pour les comparer à un modèle établi précédemment.

Mon service crée un LinearLayout de 1x1pixel au dessus des applications actives et catch les actions extérieures.

Problème : L'application plante
Code : Sélectionner tout - Visualiser dans une fenêtre à part
ava.lang.RuntimeException: Unable to start service my.utar.phonesecurat.AuthenticationCheck@4e2a62b with Intent { cmp=my.utar.phonesecurat/.AuthenticationCheck }: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
De ce que j'ai pu comprendre sur internet c'est à la fois un problème de permission Android et d'utilisation de contextes. Impossible de trouver la bonne méthode après recherches.

Voici le code de mon service, si vous avez connaissance de ce problème ou des pistes à explorer n’hésitez pas

Merci pour votre aide et bon dev à tous !

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package my.utar.phonesecurat;
 
import android.app.IntentService;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
 
import java.util.ArrayList;
 
public class AuthenticationCheck extends IntentService implements View.OnTouchListener {
 
    private WindowManager mWindowManager;
    private LinearLayout mLinearLayout;
 
    private GestureDetector gestureDetector;
    private GestureListener gestureListener;
    private VelocityTracker mVelocityTracker;
    private ArrayList<StructMotionElemts> mPointsList;
    private StructMotionFeaturesList mModelList;
    private UserModel mRightSwipeModel;
    private StructMotionElemts mStructMotionElemts;
    private StructMotionFeatures mStructMotionFeatures;
    private boolean mSwitch;
 
 
    public AuthenticationCheck() {
        super("AuthenticationCheck");
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(),"Authentication service started", Toast.LENGTH_SHORT).show();
        Log.v("TEST","LOG CHECK");
 
        mLinearLayout = new LinearLayout(getApplicationContext());
        LayoutParams mLayoutParams = new LayoutParams(1,1);
        mLinearLayout.setLayoutParams(mLayoutParams);
        mLinearLayout.setOnTouchListener(this);
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
                1,
                1,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
        );
        mParams.gravity = Gravity.LEFT | Gravity.TOP;
        mWindowManager.addView(mLinearLayout, mParams);
 
        gestureListener = new GestureListener() {
            @Override
            public void onSwipeRight() {
                super.onSwipeRight();
            }
        };
        mRightSwipeModel = new UserModel();
        //TODO retrieve UserModel saved
        mSwitch = false;
 
        gestureDetector = new GestureDetector(getApplicationContext(), gestureListener);
        Log.v("TEST","GESTURE DETECTOR CREATED");
 
 
 
 
        return START_REDELIVER_INTENT;
    }
 
    public boolean onTouch(View v, MotionEvent event) {
        Log.v("TEST","ENTERED ONTOUCH");
        return gestureDetector.onTouchEvent(event);
    }
 
    public boolean onTouchEvent(MotionEvent event) {
        Log.v("TEST","ENTERED ONTOUCHEVENT");
        switch (event.getActionMasked()) {
/**
 * ACTION_DOWN is the first touch of the screen
 * ACTION_UP is the last touch of the screen
 * ACTION_MOVE is all the intermediate points in between
 * We focus on ACTION_MOVE because on ACTION_DOWN / UP The speed can be 0 and mess the average
 */
            case MotionEvent.ACTION_DOWN:
                break;
 
            case MotionEvent.ACTION_MOVE:
                if (!mSwitch) {
                    //INITIALISATION
                    if (mVelocityTracker == null) {
                        mVelocityTracker = VelocityTracker.obtain();
                    } else {
                        mVelocityTracker.clear();
                    }
                    //Creation or reinitialisation du List
                    if (mPointsList == null) {
                        mPointsList = new ArrayList<>();
                    } else {
                        mPointsList.clear();
                    }
                    //Creation or reinitialisation du StructMotionElemts
                    if (mStructMotionElemts == null) {
                        mStructMotionElemts = new StructMotionElemts();
                    } else {
                        mStructMotionElemts.clear();
                    }
                    //Creation or reinitialisation du StructMotionFeatures
                    if (mStructMotionFeatures == null) {
                        mStructMotionFeatures = new StructMotionFeatures();
                    } else {
                        mStructMotionFeatures.clear();
                    }
                    mSwitch = true;
                }
                if(mSwitch) {
                    mStructMotionElemts.compute(event, mPointsList, mVelocityTracker);
                }
                break;
 
            case MotionEvent.ACTION_UP:
                if(mSwitch) {
                    mStructMotionFeatures.compute(mPointsList);
                    mSwitch = false;
                }
                break;
        }
        //Used at the end of a move to trigger OnFling method
        return gestureDetector.onTouchEvent(event);
    }
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
 
    @Override
    protected void onHandleIntent(Intent intent) {
    }
 
 
    @Override
    public void onDestroy() {
        if(mWindowManager != null) {
            if(mLinearLayout != null) mWindowManager.removeView(mLinearLayout);
        }
        Toast.makeText(this,"Authentication service stopped", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
}