summaryrefslogtreecommitdiffstats
path: root/policy/src/com/android/internal/policy/impl/FingerUnlockScreen.java
blob: 43ec5786a2b71ca0925fcee05fc18eaa148a59b1 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.policy.impl;

import dalvik.system.DexClassLoader;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.TextView;
import android.text.format.DateFormat;
import android.text.TextUtils;
import android.util.Log;
import com.android.internal.R;
import com.android.internal.telephony.IccCard;
import com.android.internal.widget.LinearLayoutWithDefaultTouchRecepient;
import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.LockPatternView;
import com.android.internal.widget.LockPatternView.Cell;

import java.util.List;
import java.util.Date;

// For the finger keyguard
import android.os.Handler;
import android.os.Vibrator;
import android.os.PowerManager;
import android.os.Message;
import android.os.SystemProperties;

import com.authentec.AuthentecHelper;
import com.authentec.GfxEngineRelayService;


/**
 * This is the screen that shows the 9 circle unlock widget and instructs
 * the user how to unlock their device, or make an emergency call.
 */
class FingerUnlockScreen extends LinearLayoutWithDefaultTouchRecepient
        implements KeyguardScreen, KeyguardUpdateMonitor.InfoCallback,
        KeyguardUpdateMonitor.SimStateCallback,
        GfxEngineRelayService.Receiver {

    private static final boolean DEBUG = true;
    private static final boolean DEBUG_CONFIGURATION = true;
    private static final String TAG = "FingerUnlockScreen";

    // how long we stay awake once the user is ready to enter a pattern
    private static final int UNLOCK_FINGER_WAKE_INTERVAL_MS = 7000;

    private int mFailedPatternAttemptsSinceLastTimeout = 0;
    private int mTotalFailedPatternAttempts = 0;
    private CountDownTimer mCountdownTimer = null;

    private CountDownTimer mCountdownTimerToast = null;
    private final LockPatternUtils mLockPatternUtils;
    private final KeyguardUpdateMonitor mUpdateMonitor;
    private final KeyguardScreenCallback mCallback;

    /**
     * whether there is a fallback option available when the pattern is forgotten.
     */
    private boolean mEnableFallback;

    private static boolean m_bVerifyied;

    // Do we still hold a wakelock?
    // Initialize this value to "true" since the screen is off at the very beginning.
    private static boolean m_bPaused = true;

    // The key guard would be put into lockout state every four bad swipes.
    private static boolean m_bAttemptLockout;

    // A flag to indicate if you are currently connected to the GfxEngine or not.
    private boolean m_bGfxEngineAttached;

    // A flag to indicate that there is a delayed #cancel command to send.
    private boolean m_bSendDelayedCancel;

    // Make sure that you do not double-cancel.
    private boolean m_bCancelHasBeenSent;

    // A flag to indicate that there is a successive power on to the previous power off.
    // If the previous #cancel command has not terminate the verify runner yet, set this
    // flag to let the verify runner start again.
    private boolean m_bStartAgain;

    // Below four are related with tactile feedback...
    // A flag to indicate if tactile feedback is supported.
    private boolean mTactileFeedbackEnabled = false;
    // Vibrator pattern for creating a tactile bump
    private static final long[] DEFAULT_VIBE_PATTERN = {0, 1, 40, 41};
    // Vibrator for creating tactile feedback
    private Vibrator vibe;
    // Vibration pattern, either default or customized
    private long[] mVibePattern;

    private String mDateFormatString;

    private TextView mCarrier;
    private TextView mDate;

    // are we showing battery information?
    private boolean mShowingBatteryInfo = false;

    // last known plugged in state
    private boolean mPluggedIn = false;

    // last known battery level
    private int mBatteryLevel = 100;

    private String mNextAlarm = null;

    private String mInstructions = null;
    private TextView mStatus1;
    private TextView mStatusSep;
    private TextView mStatus2;
    private TextView mUserPrompt;
    private TextView mErrorPrompt;

    private ViewGroup mFooterNormal;
    private ViewGroup mFooterForgotPattern;

    /**
     * Keeps track of the last time we poked the wake lock during dispatching
     * of the touch event, initalized to something gauranteed to make us
     * poke it when the user starts drawing the pattern.
     * @see #dispatchTouchEvent(android.view.MotionEvent)
     */
    private long mLastPokeTime = -UNLOCK_FINGER_WAKE_INTERVAL_MS;

    /**
     * Useful for clearing out the wrong pattern after a delay
     */
    private Runnable mCancelPatternRunnable = new Runnable() {
        public void run() {
            //mLockPatternView.clearPattern();
        }
    };

    private Button mForgotPatternButton;
    private Button mEmergencyAlone;
    private Button mEmergencyTogether;
    private int mCreationOrientation;

    static Thread mExecutionThread = null;
    private Thread mUiThread;
    private boolean mbInvalidSwipe = false;
    private VerifyRunner mVerifyRunner = new VerifyRunner();
    private Context m_Context;

    /** High level access to the power manager for WakeLocks */
    private PowerManager mPM;

    /**
     * Used to keep the device awake while the keyguard is showing, i.e for
     * calls to {@link #pokeWakelock()}
     * Note: Unlike the mWakeLock in KeyguardViewMediator (which has the
     *       ACQUIRE_CAUSES_WAKEUP flag), this one doesn't actually turn
     *       on the illumination. Instead, they cause the illumination to
     *       remain on once it turns on.
     */
    private PowerManager.WakeLock mWakeLock;

    /**
     * Does not turn on screen, used to keep the device awake until the
     * fingerprint verification is ready. The KeyguardViewMediator only
     * poke the wake lock for 5 seconds, which is not enough for the
     * initialization of the fingerprint verification.
     */
    private PowerManager.WakeLock mHandOffWakeLock;

    private int mWakelockSequence;

    // used for handler messages
    private static final int TIMEOUT = 1;

    enum FooterMode {
        Normal,
        ForgotLockPattern,
        VerifyUnlocked
    }

    private void updateFooter(FooterMode mode) {
        switch (mode) {
            case Normal:
                mFooterNormal.setVisibility(View.VISIBLE);
                mFooterForgotPattern.setVisibility(View.GONE);
                break;
            case ForgotLockPattern:
                mFooterNormal.setVisibility(View.GONE);
                mFooterForgotPattern.setVisibility(View.VISIBLE);
                mForgotPatternButton.setVisibility(View.VISIBLE);
                break;
            case VerifyUnlocked:
                mFooterNormal.setVisibility(View.GONE);
                mFooterForgotPattern.setVisibility(View.GONE);
        }
    }

    private AuthentecHelper fingerhelper = null;

    /**
     * @param context The context.
     * @param lockPatternUtils Used to lookup lock pattern settings.
     * @param updateMonitor Used to lookup state affecting keyguard.
     * @param callback Used to notify the manager when we're done, etc.
     * @param totalFailedAttempts The current number of failed attempts.
     * @param enableFallback True if a backup unlock option is available when the user has forgotten
     *        their pattern (e.g they have a google account so we can show them the account based
     *        backup option).
     */
    FingerUnlockScreen(Context context,
                 Configuration configuration,
                 LockPatternUtils LockPatternUtils,
                 KeyguardUpdateMonitor updateMonitor,
                 KeyguardScreenCallback callback,
                 int totalFailedAttempts) {
        super(context);

        fingerhelper = AuthentecHelper.getInstance(context);

        m_Context = context;

        /* goToSleep of PowerManagerService class would cancel all of the wake locks. */
        mPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = mPM.newWakeLock(
                PowerManager.FULL_WAKE_LOCK,
                "FpKeyguard");
        mWakeLock.setReferenceCounted(false);
        mHandOffWakeLock = mPM.newWakeLock(
                PowerManager.FULL_WAKE_LOCK,
                "FpKeyguardHandOff");
        mHandOffWakeLock.setReferenceCounted(false);

        mLockPatternUtils = LockPatternUtils;
        mUpdateMonitor = updateMonitor;
        mCallback = callback;
        mTotalFailedPatternAttempts = totalFailedAttempts;
        mFailedPatternAttemptsSinceLastTimeout =
            totalFailedAttempts % mLockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT;

        if (DEBUG) Log.d(TAG,
            "UnlockScreen() ctor: totalFailedAttempts="
                 + totalFailedAttempts + ", mFailedPat...="
                 + mFailedPatternAttemptsSinceLastTimeout
                 );

        m_bVerifyied = false;
        m_bAttemptLockout = false;

        m_bGfxEngineAttached = false;
        m_bSendDelayedCancel = false;
        m_bCancelHasBeenSent = false;
        m_bStartAgain = false;

        mTactileFeedbackEnabled = mLockPatternUtils.isTactileFeedbackEnabled();
        if (mTactileFeedbackEnabled) {
            if (DEBUG) Log.d(TAG, "Create a vibrator");
            vibe = new Vibrator();
        }

        mCreationOrientation = configuration.orientation;

        LayoutInflater inflater = LayoutInflater.from(context);
        if (mCreationOrientation != Configuration.ORIENTATION_LANDSCAPE) {
            if (DEBUG) Log.d(TAG, "UnlockScreen() : portrait");
            inflater.inflate(R.layout.keyguard_screen_finger_portrait, this, true);
        } else {
            if (DEBUG) Log.d(TAG, "UnlockScreen() : landscape");
            inflater.inflate(R.layout.keyguard_screen_finger_landscape, this, true);
        }

        mCarrier = (TextView) findViewById(R.id.carrier);
        mDate = (TextView) findViewById(R.id.date);

        mDateFormatString = getContext().getString(R.string.full_wday_month_day_no_year);
        refreshTimeAndDateDisplay();

        mStatus1 = (TextView) findViewById(R.id.status1);
        mStatusSep = (TextView) findViewById(R.id.statusSep);
        mStatus2 = (TextView) findViewById(R.id.status2);

        resetStatusInfo();

        mUserPrompt = (TextView) findViewById(R.id.usageMessage);
        // Temporary hints for the emulator.
        mUserPrompt.setText(R.string.keyguard_finger_screen_off);
        mErrorPrompt = (TextView) findViewById(R.id.errorMessage);

        mFooterNormal = (ViewGroup) findViewById(R.id.footerNormal);
        mFooterForgotPattern = (ViewGroup) findViewById(R.id.footerForgotPattern);

        mUiThread = Thread.currentThread();
        GfxEngineRelayService.setLocalReceiver(this);

        // emergency call buttons
        final OnClickListener emergencyClick = new OnClickListener() {
            public void onClick(View v) {
                // Cancel the Verify thread.
                mUserPrompt.setText("");
                if (mExecutionThread != null && mExecutionThread.isAlive()) {
                    if (!m_bGfxEngineAttached) {
                        if (DEBUG) Log.d(TAG,"emergencyClick send cancel delayed");
                        m_bSendDelayedCancel = true;
                    } else {
                        if (DEBUG) Log.d(TAG,"emergencyClick send cancel");
                        if (!m_bCancelHasBeenSent)
                        {
                            GfxEngineRelayService.queueEvent("#cancel");
                            m_bCancelHasBeenSent = true;
                        }
                    }
                }
                mCallback.takeEmergencyCallAction();
            }
        };

        mEmergencyAlone = (Button) findViewById(R.id.emergencyCallAlone);
        mEmergencyAlone.setFocusable(false); // touch only!
        mEmergencyAlone.setOnClickListener(emergencyClick);
        mEmergencyTogether = (Button) findViewById(R.id.emergencyCallTogether);
        mEmergencyTogether.setFocusable(false);
        mEmergencyTogether.setOnClickListener(emergencyClick);
        refreshEmergencyButtonText();

        mForgotPatternButton = (Button) findViewById(R.id.forgotPattern);
        mForgotPatternButton.setText(R.string.lockscreen_forgot_finger_button_text);
        mForgotPatternButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mCallback.forgotPattern(true);
            }
        });

        if (mTactileFeedbackEnabled) {
            // allow vibration pattern to be customized
            if (DEBUG) Log.d(TAG, "Load vibration pattern");
            mVibePattern = loadVibratePattern(com.android.internal.R.array.config_virtualKeyVibePattern);
        }

        // assume normal footer mode for now
        updateFooter(FooterMode.Normal);

        updateMonitor.registerInfoCallback(this);
        updateMonitor.registerSimStateCallback(this);
        setFocusableInTouchMode(true);

        // Required to get Marquee to work.
        mCarrier.setSelected(true);
        mCarrier.setTextColor(0xffffffff);

        // until we get an update...
        mCarrier.setText(
                LockScreen.getCarrierString(
                        mUpdateMonitor.getTelephonyPlmn(),
                        mUpdateMonitor.getTelephonySpn()));
    }

    private void refreshEmergencyButtonText() {
        mLockPatternUtils.updateEmergencyCallButtonState(mEmergencyAlone);
        mLockPatternUtils.updateEmergencyCallButtonState(mEmergencyTogether);
    }

    public void setEnableFallback(boolean state) {
        if (DEBUG) Log.d(TAG, "setEnableFallback(" + state + ")");
        mEnableFallback = state;
    }

    private void resetStatusInfo() {
        mInstructions = null;
        mShowingBatteryInfo = mUpdateMonitor.shouldShowBatteryInfo();
        mPluggedIn = mUpdateMonitor.isDevicePluggedIn();
        mBatteryLevel = mUpdateMonitor.getBatteryLevel();
        mNextAlarm = mLockPatternUtils.getNextAlarm();
        updateStatusLines();
    }

    private void updateStatusLines() {
        if (mInstructions != null) {
            // instructions only
            if (DEBUG) Log.d(TAG,"instructions only");
            mStatus1.setText(mInstructions);
            if (TextUtils.isEmpty(mInstructions)) {
                mStatus1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            } else {
                mStatus1.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_lock_idle_lock, 0, 0, 0);
            }

            mStatus1.setVisibility(View.VISIBLE);
            mStatusSep.setVisibility(View.GONE);
            mStatus2.setVisibility(View.GONE);
        } else if (mShowingBatteryInfo && mNextAlarm == null) {
            // battery only
            if (DEBUG) Log.d(TAG,"battery only");
            if (mPluggedIn) {
              if (mBatteryLevel >= 100) {
                mStatus1.setText(getContext().getString(R.string.lockscreen_charged));
              } else {
                  mStatus1.setText(getContext().getString(R.string.lockscreen_plugged_in, mBatteryLevel));
              }
            } else {
                mStatus1.setText(getContext().getString(R.string.lockscreen_low_battery, mBatteryLevel));
            }
            mStatus1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_idle_charging, 0, 0, 0);

            mStatus1.setVisibility(View.VISIBLE);
            mStatusSep.setVisibility(View.GONE);
            mStatus2.setVisibility(View.GONE);

        } else if (mNextAlarm != null && !mShowingBatteryInfo) {
            // alarm only
            if (DEBUG) Log.d(TAG,"alarm only");
            mStatus1.setText(mNextAlarm);
            mStatus1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_idle_alarm, 0, 0, 0);

            mStatus1.setVisibility(View.VISIBLE);
            mStatusSep.setVisibility(View.GONE);
            mStatus2.setVisibility(View.GONE);
        } else if (mNextAlarm != null && mShowingBatteryInfo) {
            // both battery and next alarm
            if (DEBUG) Log.d(TAG,"both battery and next alarm");
            mStatus1.setText(mNextAlarm);
            mStatusSep.setText("|");
            mStatus2.setText(getContext().getString(
                    R.string.lockscreen_battery_short,
                    Math.min(100, mBatteryLevel)));
            mStatus1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_idle_alarm, 0, 0, 0);
            if (mPluggedIn) {
                mStatus2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_idle_charging, 0, 0, 0);
            } else {
                mStatus2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }

            mStatus1.setVisibility(View.VISIBLE);
            mStatusSep.setVisibility(View.VISIBLE);
            mStatus2.setVisibility(View.VISIBLE);
        } else {
            // nothing specific to show; show general instructions
            if (DEBUG) Log.d(TAG,"nothing specific to show");
            // "keyguard_finger_please_swipe" string would be showed when the TSM is ready.
            //mStatus1.setText(R.string.lockscreen_pattern_instructions);
            mStatus1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_idle_lock, 0, 0, 0);

            mStatus1.setVisibility(View.VISIBLE);
            mStatusSep.setVisibility(View.GONE);
            mStatus2.setVisibility(View.GONE);
        }
    }


    private void refreshTimeAndDateDisplay() {
        mDate.setText(DateFormat.format(mDateFormatString, new Date()));
    }

    private long[] loadVibratePattern(int id) {
        int[] pattern = null;
        try {
            pattern = getResources().getIntArray(id);
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Vibrate pattern missing, using default", e);
        }
        if (pattern == null) {
            return DEFAULT_VIBE_PATTERN;
        }

        long[] tmpPattern = new long[pattern.length];
        for (int i = 0; i < pattern.length; i++) {
            tmpPattern[i] = pattern[i];
        }
        return tmpPattern;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // as long as the user is entering a pattern (i.e sending a touch
        // event that was handled by this screen), keep poking the
        // wake lock so that the screen will stay on.
        final boolean result = super.dispatchTouchEvent(ev);
        if (result &&
                ((SystemClock.elapsedRealtime() - mLastPokeTime)
                        >  (UNLOCK_FINGER_WAKE_INTERVAL_MS - 100))) {
            mLastPokeTime = SystemClock.elapsedRealtime();
        }
        return result;
    }


    // ---------- InfoCallback

    /** {@inheritDoc} */
    public void onRefreshBatteryInfo(boolean showBatteryInfo, boolean pluggedIn, int batteryLevel) {
        mShowingBatteryInfo = showBatteryInfo;
        mPluggedIn = pluggedIn;
        mBatteryLevel = batteryLevel;
        updateStatusLines();
    }

    /** {@inheritDoc} */
    public void onTimeChanged() {
        refreshTimeAndDateDisplay();
    }

    /** {@inheritDoc} */
    public void onRefreshCarrierInfo(CharSequence plmn, CharSequence spn) {
        mCarrier.setText(LockScreen.getCarrierString(plmn, spn));
    }

    /** {@inheritDoc} */
    public void onRingerModeChanged(int state) {
        // not currently used
    }

    // ---------- SimStateCallback

    /** {@inheritDoc} */
    public void onSimStateChanged(IccCard.State simState) {
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (DEBUG_CONFIGURATION) {
            Log.v(TAG, "***** FINGERPRINT ATTACHED TO WINDOW");
            Log.v(TAG, "Cur orient=" + mCreationOrientation
                    + ", new config=" + getResources().getConfiguration());
        }
        if (getResources().getConfiguration().orientation != mCreationOrientation) {
            mCallback.recreateMe(getResources().getConfiguration());
        }
    }


    /** {@inheritDoc} */
    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (DEBUG_CONFIGURATION) {
            Log.v(TAG, "***** FINGERPRINT CONFIGURATION CHANGED");
            Log.v(TAG, "Cur orient=" + mCreationOrientation
                    + ", new config=" + getResources().getConfiguration());
        }
        if (newConfig.orientation != mCreationOrientation) {
            if (DEBUG) Log.d(TAG,"Orientation changed, recreateMe");
            mCallback.recreateMe(newConfig);
        }
    }

    /** {@inheritDoc} */
    public void onKeyboardChange(boolean isKeyboardOpen) {}

    /** {@inheritDoc} */
    public boolean needsInput() {
        return false;
    }

    /** {@inheritDoc} */
    public void onPause() {
        if (DEBUG) Log.d(TAG,"onPause");

        // Temporary hints for the emulator.
          mUserPrompt.setText(R.string.keyguard_finger_screen_off);
        mErrorPrompt.setText("");

        if (mWakeLock.isHeld()) {
            /* Must release since it is a screen lock. */
            mHandler.removeMessages(TIMEOUT);
            mWakeLock.release();
        }

        if (mHandOffWakeLock.isHeld()) {
            /* Must release since it is a screen lock. */
            mHandOffWakeLock.release();
        }

        m_bVerifyied = false;
        m_bPaused = true;
        m_bStartAgain = false;
        if (mCountdownTimer != null) {
            mCountdownTimer.cancel();
            mCountdownTimer = null;
        }
        if (mCountdownTimerToast != null) {
            mCountdownTimerToast.cancel();
            mCountdownTimerToast = null;
        }
        if (mExecutionThread != null && mExecutionThread.isAlive()) {
            if (!m_bGfxEngineAttached) {
                if (DEBUG) Log.d(TAG,"onPause send cancel delayed");
                m_bSendDelayedCancel = true;
            } else {
                if (DEBUG) Log.d(TAG,"onPause send cancel");
                if (!m_bCancelHasBeenSent)
                {
                    GfxEngineRelayService.queueEvent("#cancel");
                    m_bCancelHasBeenSent = true;
                }
            }
        }
    }

    private void resumeViews() {
        if ((mExecutionThread!=null)&&(mExecutionThread.isAlive())) {
            if (DEBUG) Log.d(TAG,"resumeViews: Thread in execution");
            return;
        }

        if (!mLockPatternUtils.savedFingerExists()) {
            if (DEBUG) Log.d(TAG,"resumeViews: No saved finger");
            // By design, this situation should never happen.
            // If finger lock is in use, we should only allow the finger settings menu
            // to delete all enrolled fingers. Other applications, like TSMDemo, should
            // not get access to the database.
            //
            // Simply disable the finger lock and exit.
            mLockPatternUtils.setLockFingerEnabled(false);
            return;
        }

        // reset header
        resetStatusInfo();
        if (DEBUG) Log.d(TAG,"resumeViews: m_bVerifyied=" + m_bVerifyied);

        // show "forgot pattern?" button if we have an alternate authentication method
        mForgotPatternButton.setVisibility(View.INVISIBLE);

        // if the user is currently locked out, enforce it.
        long deadline = mLockPatternUtils.getLockoutAttemptDeadline();
        if (deadline != 0) {
            if (DEBUG) Log.d(TAG,"resumeViews: In lockout state");
            handleAttemptLockout(deadline);
        } else {
            // The onFinish() method of the CountDownTimer object would not be
            // called when the screen is off. So we need to reset the m_bAttemptLockout
            // if the lockout has finished.
            m_bAttemptLockout = false;
        }

        // the footer depends on how many total attempts the user has failed
        if (mCallback.isVerifyUnlockOnly()) {
            updateFooter(FooterMode.VerifyUnlocked);
        } else if (mEnableFallback &&
                (mTotalFailedPatternAttempts >= mLockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT)) {
            updateFooter(FooterMode.ForgotLockPattern);
        } else {
            updateFooter(FooterMode.Normal);
        }

        refreshEmergencyButtonText();
    }

    /** {@inheritDoc} */
    public void onResume() {
        // Resume the finger unlock screen.
        resumeViews();

        // Launch a TSM Verify thread when the screen is turned on.
        if (mPM.isScreenOn()) {
            onScreenOn();
        }
    }

    private void onScreenOn() {
        if (DEBUG) Log.d(TAG,"onScreenOn()");

        m_bPaused = false;
        if (m_bSendDelayedCancel) {
            /* If "cancel" has not been sent out, simply ignore it. */
            m_bSendDelayedCancel = false;
        }

        /* Thread in execution, no need to create & start again. */
        if ((mExecutionThread!=null)&&(mExecutionThread.isAlive())) {
            if (DEBUG) Log.d(TAG,"onScreenOn: Thread in execution");
            if (m_bCancelHasBeenSent) {
                /* If "cancel" has been sent, set this flag to indicate the verify runner to start again. */
                m_bStartAgain = true;
            }
            return;
        }

        // Temporary hints for the emulator.
        mUserPrompt.setText(R.string.keyguard_finger_screen_on);

        if (mLockPatternUtils.isLockFingerEnabled()&&!m_bVerifyied) {
            /**
             * acquire the handoff lock that will keep the cpu running. this will
             * be released once the fingerprint keyguard has set the verification up
             * and poked the mWakelock of itself (not the one of the KeyguradViewMediator).
             */
            mHandOffWakeLock.acquire();

            // Create a thread for verification.
            mExecutionThread = new Thread(mVerifyRunner);

            // Only start the verification when the screen is not in lockout state.
            if (!m_bAttemptLockout)
            {
                mExecutionThread.start();
            }
            else
            {
                // Release the wakelock early if the screen is in lockout state now.
                pokeWakelock(1000);
            }
        }
    }

    /** {@inheritDoc} */
    public void cleanUp() {
        if (mExecutionThread != null && mExecutionThread.isAlive()) {
            if (!m_bGfxEngineAttached) {
                if (DEBUG) Log.d(TAG,"cleanUp send cancel delayed");
                m_bSendDelayedCancel = true;
            } else {
                if (DEBUG) Log.d(TAG,"cleanUp send cancel");
                if (!m_bCancelHasBeenSent)
                {
                    GfxEngineRelayService.queueEvent("#cancel");
                    m_bCancelHasBeenSent = true;
                }
            }
        }

        // must make sure that the verify runner has terminated.
        while (mExecutionThread != null && mExecutionThread.isAlive()) {
            try {
                // Set a flag to indicate the verify runner to terminate itself.
                m_bPaused = true;
                mUiThread.sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        mUpdateMonitor.removeCallback(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            // when timeout dialog closes we want to update our state
            //onResume();
            if (DEBUG) Log.d(TAG,"onWindowFocusChanged");
            resumeViews();

            // Start the verification if screen is on.
            if (mPM.isScreenOn()) {
                if ( (!m_bAttemptLockout) && (!m_bPaused) &&
                     (mExecutionThread != null) && (!(mExecutionThread.isAlive())) )
                {
                    if (DEBUG) Log.d(TAG,"Screen is on, start LAP verification");
                    /**
                     * acquire the handoff lock that will keep the cpu running. this will
                     * be released once the fingerprint keyguard has set the verification up
                     * and poked the mWakelock of itself (not the one of the KeyguradViewMediator).
                     */
                    mHandOffWakeLock.acquire();

                    if (mExecutionThread.getState() == Thread.State.TERMINATED)
                    {
                        // If the thread state is TERMINATED, it cannot be start() again.
                        // Create a thread for verification.
                        mExecutionThread = new Thread(mVerifyRunner);
                        mExecutionThread.start();
                    }
                    else
                    {
                        if (DEBUG) Log.d(TAG,"Verify thread exists, just start it");
                        mExecutionThread.start();
                    }
                }
            }
        }
    }

    public final void runOnUiThread(Runnable action) {
        if (Thread.currentThread() != mUiThread) {
            mHandler .post(action);
        } else {
            action.run();
        }
    }

    private void pokeWakelock(int holdMs) {
        synchronized (this) {
            if (DEBUG) Log.d(TAG, "pokeWakelock(" + holdMs + ")");
            mWakeLock.acquire();
            mHandler.removeMessages(TIMEOUT);
            mWakelockSequence++;
            Message msg = mHandler.obtainMessage(TIMEOUT, mWakelockSequence, 0);
            mHandler.sendMessageDelayed(msg, holdMs);
        }

        if (mHandOffWakeLock.isHeld()) {
           /**
            * The fingerprint keyguard has been set up, and has poked the keyguard
            * main wake lock. It's time to release the handoff wake lock.
            */
           mHandOffWakeLock.release();
        }
    }

    /**
     * This handler will be associated with the policy thread, which will also
     * be the UI thread of the keyguard.  Since the apis of the policy, and therefore
     * this class, can be called by other threads, any action that directly
     * interacts with the keyguard ui should be posted to this handler, rather
     * than called directly.
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case TIMEOUT:
                    handleTimeout(msg.arg1);
                    return ;
            }
        }
    };

    /**
     * Handles the message sent by {@link #pokeWakelock}
     * @param seq used to determine if anything has changed since the message
     *   was sent.
     * @see #TIMEOUT
     */
    private void handleTimeout(int seq) {
        synchronized (this) {
            if (DEBUG) Log.d(TAG, "handleTimeout");
            if (seq == mWakelockSequence) {
                mWakeLock.release();
            }
        }
    }

    /**
      * Provides a Runnable class to handle the finger
      * verification
      */
    private class VerifyRunner implements Runnable {
        public void run() {
            int iResult = 0;
            boolean bRetryAfterLockout = false;

            m_bVerifyied = false;

            // A trick to dismiss the timeout dialog.
            mCallback.isVerifyUnlockOnly();

            try {
                while (true) {
                    if (m_bPaused) {
                        if (DEBUG) Log.d(TAG,"VerifyRunner: paused before the verify() call.");
                        /* Do not call the verify interface if the "cancel" comes before it starts. */
                        if (AuthentecHelper.eAM_STATUS_OK == iResult) {
                            /* The result should never be eAM_STATUS_OK. */
                            iResult = AuthentecHelper.eAM_STATUS_USER_CANCELED;
                        }
                        break;
                    }

                    iResult = fingerhelper.verifyPolicy(m_Context);

                    if (DEBUG) Log.d(TAG,"Verify result=" + iResult);
                    if (AuthentecHelper.eAM_STATUS_CREDENTIAL_LOCKED == iResult) {
                        Log.e(TAG, "Credential locked!");
                        //runOnUiThread(new Runnable() {
                            //public void run() {
                                //toast(getContext().getString(R.string.keyguard_finger_failed_to_unlock));
                            //}
                        //});
                    } else if (AuthentecHelper.eAM_STATUS_USER_CANCELED == iResult) {
                        if (m_bStartAgain) {
                            Log.e(TAG, "Cancel OK, continue because of the successive launch.");
                            m_bStartAgain = false;
                            m_bPaused = false;
                        } else {
                            break;
                        }
                    } else {
                        // Terminate the current thread for all other cases.
                        break;
                    }

                    if (m_bPaused) {
                        /* Break immediatly without the sleep. */
                        if (DEBUG) Log.d(TAG,"VerifyRunner: paused after the verify() call.");
                        break;
                    }

                    // Give other tasks a chance.
                    Thread.sleep(10);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                switch (iResult) {
                    case AuthentecHelper.eAM_STATUS_NO_STORED_CREDENTIAL:
                        // Might happen if the user wipes their database
                        // and the fingerprint unlock method remains active.
                        // Let it continue with the OK case so screen unlocks
                        if (DEBUG) Log.d(TAG,"No stored credential");

                    case AuthentecHelper.eAM_STATUS_OK:
                        m_bVerifyied = true;
                        if (DEBUG) Log.d(TAG,"keyguardDone, m_bVerifyied=" + m_bVerifyied);
                        mCallback.keyguardDone(true);
                        mHandler.removeMessages(TIMEOUT);
                        mWakeLock.release();
                        break;

                    case AuthentecHelper.eAM_STATUS_USER_CANCELED:
                        // Power off.
                        Log.e(TAG, "Simulating device lock.\nYou may not cancel!");
                        break;

                    case AuthentecHelper.eAM_STATUS_CREDENTIAL_LOCKED:
                        // When m_bPaused becomes true.
                        bRetryAfterLockout = true;
                        break;

                    case AuthentecHelper.eAM_STATUS_LIBRARY_NOT_AVAILABLE:
                        Log.e(TAG, "Library failed to load... cannot proceed!");
                        bRetryAfterLockout = true;
                        break;

                    case AuthentecHelper.eAM_STATUS_UI_TIMEOUT:
                        Log.e(TAG, "UI timeout!");
                        runOnUiThread(new Runnable() {
                            public void run() {
                                toast(getContext().getString(R.string.keyguard_finger_ui_timeout));
                            }
                        });
                        bRetryAfterLockout = true;
                        break;

                    case AuthentecHelper.eAM_STATUS_UNKNOWN_ERROR:
                        Log.e(TAG, "Unknown error!");
                        bRetryAfterLockout = true;
                        break;

                    default:
                        Log.e(TAG, "Other results: " + iResult);
                        bRetryAfterLockout = true;
                }
            } catch (Exception e){}

            if (!m_bPaused && bRetryAfterLockout) {
                if (DEBUG) Log.d(TAG,"Error happens, retry after lock out");
                runOnUiThread(new Runnable() {
                    public void run() {
                        pokeWakelock(1000);
                        long deadline = mLockPatternUtils.setLockoutAttemptDeadline();
                        handleAttemptLockout(deadline);
                    }
                });
            }
        }
    }

    private void handleAttemptLockout(long elapsedRealtimeDeadline) {
        // Put the key guard into lockout state.
        m_bAttemptLockout = true;
        // Cancel the Verify thread.
        if (mExecutionThread != null && mExecutionThread.isAlive()) {
            if (!m_bGfxEngineAttached) {
                if (DEBUG) Log.d(TAG,"Lockout send cancel delayed");
                m_bSendDelayedCancel = true;
            } else {
                if (DEBUG) Log.d(TAG,"Lockout send cancel");
                if (!m_bCancelHasBeenSent)
                {
                    GfxEngineRelayService.queueEvent("#cancel");
                    m_bCancelHasBeenSent = true;
                }
            }
        }

        long elapsedRealtime = SystemClock.elapsedRealtime();
        mCountdownTimer = new CountDownTimer(elapsedRealtimeDeadline - elapsedRealtime, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                int secondsRemaining = (int) (millisUntilFinished / 1000);
                //mInstructions = getContext().getString(
                //        R.string.lockscreen_too_many_failed_attempts_countdown,
                //        secondsRemaining);
                //updateStatusLines();
                mUserPrompt.setText(getContext().getString(
                        R.string.lockscreen_too_many_failed_attempts_countdown,
                        secondsRemaining));
            }

            @Override
            public void onFinish() {
                // Put the key guard out of lockout state.
                if (DEBUG) Log.d(TAG,"handleAttemptLockout: onFinish");
                m_bAttemptLockout = false;
                // "keyguard_finger_please_swipe" string would be showed when the TSM is ready.
                // mInstructions = getContext().getString(R.string.lockscreen_pattern_instructions);
                //updateStatusLines();
                resetStatusInfo();
                // TODO mUnlockIcon.setVisibility(View.VISIBLE);
                mFailedPatternAttemptsSinceLastTimeout = 0;
                if (mEnableFallback) {
                    updateFooter(FooterMode.ForgotLockPattern);
                } else {
                    updateFooter(FooterMode.Normal);
                }

                // Show the "Screen off" status
                if (m_bPaused) {
                    mUserPrompt.setText(R.string.keyguard_finger_screen_off);
                } else {
                    mUserPrompt.setText("");
                }

                // Start the verification if the finger key guard is holding the wakelock.
                if ( (!m_bPaused) && (mExecutionThread != null) && (!(mExecutionThread.isAlive())) ) {
                    /**
                     * acquire the handoff lock that will keep the cpu running. this will
                     * be released once the fingerprint keyguard has set the verification up
                     * and poked the mWakelock of itself (not the one of the KeyguradViewMediator).
                     */
                    mHandOffWakeLock.acquire();
                    if (mExecutionThread.getState() == Thread.State.TERMINATED) {
                        // If the thread state is TERMINATED, it cannot be start() again.
                        // Create a thread for verification.
                        mExecutionThread = new Thread(mVerifyRunner);
                        mExecutionThread.start();
                    } else {
                        if (DEBUG) Log.d(TAG,"Verify thread exists, just start it");
                        mExecutionThread.start();
                    }
                }
            }
        }.start();
    }

    public void onPhoneStateChanged(String newState) {
        refreshEmergencyButtonText();
    }

    /* handleShow() is called when the GfxEngine sends "show <target>" */
    private void handleShow(String target)
    {
        if (DEBUG) Log.w(TAG, "'show' target: " + target);

        // Has the object paused?
        if (m_bPaused)
        {
            if (DEBUG) Log.d(TAG,"handleShow: paused");
            return;
        }

        if (m_bAttemptLockout)
        {
            if (DEBUG) Log.d(TAG,"handleShow: Locked out");
            return;
        }

        /* if the target is C1-C10 or D1-D10, ignore the command */
        if (target.matches("[CD][1-9]0?$")) return;

        /* if the target is please_swipe, show the UI command to the user: */
        if (target.equals("please_swipe")) {
            /* update the UI */
            runOnUiThread(new Runnable() {
                public void run() {
                    mUserPrompt.setText(R.string.keyguard_finger_please_swipe);
                    // How long we stay awake once the system is ready for a user to enter a pattern.
                    pokeWakelock(UNLOCK_FINGER_WAKE_INTERVAL_MS);
                }
            });
            return;
        }

        if (target.equals("swipe_good")) {
               runOnUiThread(new Runnable() {
                public void run() {
                    toast(getContext().getString(R.string.keyguard_finger_match));
                }
            });
            return;
        }
        /* we'll be asked to show swipe_bad if the finger does not verify. */
        /* this could be following a usage feedback message, in which case */
        /* we've already displayed the feedback, so we don't want to worry */
        /* about an additional message.                                    */
        if (target.equals("swipe_bad")) {
            if (!mbInvalidSwipe) {
                // Update the total failed attempts.
                mTotalFailedPatternAttempts++;
                mFailedPatternAttemptsSinceLastTimeout++;

                runOnUiThread(new Runnable() {
                    public void run() {
                        toast(getContext().getString(R.string.keyguard_finger_not_match));
                        mCallback.reportFailedUnlockAttempt();
                        if (mFailedPatternAttemptsSinceLastTimeout >=
                                LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT) {
                            pokeWakelock(1000);
                            long deadline = mLockPatternUtils.setLockoutAttemptDeadline();
                            handleAttemptLockout(deadline);
                        }
                    }
                });
            } else {
                mbInvalidSwipe = false;
            }

            return;
        }

        /* if the target is any of our feedback messages, provide a toast... */
        if (target.equals("swipe_too_fast")) {
            mbInvalidSwipe = true;
            runOnUiThread(new Runnable() {
                public void run() {
                    toast(getContext().getString(R.string.keyguard_finger_swipe_too_fast));
                }
            });
            return;
        } else if (target.equals("swipe_too_slow")) {
            mbInvalidSwipe = true;
            runOnUiThread(new Runnable() {
                public void run() {
                    toast(getContext().getString(R.string.keyguard_finger_swipe_too_slow));
                }
            });
            return;
        } else if (target.equals("swipe_too_short")) {
            mbInvalidSwipe = true;
            runOnUiThread(new Runnable() {
                public void run() {
                    toast(getContext().getString(R.string.keyguard_finger_swipe_too_short));
                }
            });
            return;
        } else if (target.equals("swipe_too_skewed")) {
            mbInvalidSwipe = true;
            runOnUiThread(new Runnable() {
                public void run() {
                    toast(getContext().getString(R.string.keyguard_finger_swipe_too_skewed));
                }
            });
            return;
        } else if (target.equals("swipe_too_far_left")) {
            mbInvalidSwipe = true;
            runOnUiThread(new Runnable() {
                public void run() {
                    toast(getContext().getString(R.string.keyguard_finger_swipe_too_far_left));
                }
            });
            return;
        } else if (target.equals("swipe_too_far_right")) {
            mbInvalidSwipe = true;
            runOnUiThread(new Runnable() {
                public void run() {
                    toast(getContext().getString(R.string.keyguard_finger_swipe_too_far_right));
                }
            });
            return;
        }

        /* if we get here, we did not recognize the target... */
        if (DEBUG) Log.w(TAG, "Unhandled 'show' target: " + target);
    }

    /* handleHide() is called when the GfxEngine sends "hide <target>" */
    private void handleHide(String target)
    {
        // Has the object paused?
        if (m_bPaused)
        {
            if (DEBUG) Log.d(TAG,"handleHide: paused");
            return;
        }

        /* if the target is C1-C10 or D1-D10, ignore the command */
        if (target.matches("[CD][1-9]0?$")) return;

        /* if the target is please_swipe, remove the user prompt */
        if (target.equals("please_swipe"))
        {
            runOnUiThread(new Runnable() {
                public void run() {
                      mUserPrompt.setText("");
                    /**
                     * acquire the handoff lock that will keep the cpu running. this will
                     * be released once the fingerprint keyguard has set the next verification
                     * up and poked the mWakelock of itself (not the one of the KeyguradViewMediator).
                     */
                    mHandOffWakeLock.acquire();

                    if (mTactileFeedbackEnabled) {
                        // Generate tactile feedback
                        if (DEBUG) Log.d(TAG,"Finger on vibration");
                        vibe.vibrate(mVibePattern, -1);
                    }
                }
            });
            return;
        }

        /* if the target is any of our feedback messages, nothing to hide... */

        /* if we get here, we did not recognize the target... */
        if (DEBUG) Log.w(TAG, "Unhandled 'hide' target: " + target);
    }

    /* receiveCommand() is called by a package-local component of the   */
    /* relay server, any time a command is received from the GfxEngine. */
    public void receiveCommand(String command, String args) {
        /* process the command: */

        /*  for "screen" without parameters, the GfxEngine is done with us...
         *  we expect to receive exactly two screen commands during our
         *  lifetime:
         *      command=="screen", args=="<something here>"
         *  and
         *      command=="screen", args==""
         *  The first pattern will be the very first command we receive, and
         *  the second will be the very last command we receive. The argument
         *  in the first pattern will be indicative of the reason the system
         *  is presenting a UI; if our activity/service is intended to manage
         *  multiple screen types, we can use this to distinguish which one to
         *  present.
         */
        if (command.equals("screen") && ((args == null) || (args.length() == 0))) {
            // in this case we received "screen" without any arguments... if
            // our activity had been launched by the GfxEngine, we would key on
            // this to terminate. in this demo, our activity is around before
            // the GfxEngine, and we trigger it rather than it triggering us,
            // so we can safely ignore this command.

            // we just received "screen" with no arguments... the connection is breaking.
            if (DEBUG) Log.w(TAG, "receiveCommand: the connection is breaking");
            m_bGfxEngineAttached = false;
            m_bCancelHasBeenSent = false;
            return;
        }
        else if (command.equals("screen")) {
            // we don't need to do anything here unless we care about the name
            // of the screen being instantiated (in which case we are interested
            // in the value of args)

            // we're attached to the GfxEngine, manage delayed cancelation.
            if (DEBUG) Log.w(TAG, "receiveCommand: attatched to the GfxEngine");
            m_bGfxEngineAttached = true;
            if ((m_bSendDelayedCancel) && (!m_bCancelHasBeenSent)) {
                if (DEBUG) Log.w(TAG, "receiveCommand: cancel delayed");
                if (mExecutionThread != null && mExecutionThread.isAlive()) {
                    if (DEBUG) Log.w(TAG, "receiveCommand: send delayed #cancel");
                    GfxEngineRelayService.queueEvent("#cancel");
                    m_bCancelHasBeenSent = true;
                    m_bSendDelayedCancel = false;
                }
            }
            return;
        }

        /*  if the command is "show" or "hide", we're being asked to show or
         *  hide some named UI element. In some cases, the UI elements may not
         *  actually exist, in which case the command can probably be ignored.
         *  based on the name of the element to be shown / hidden, we might
         *  make different choices. An element may exist to tell the user to do
         *  something: the element 'please_swipe' is used to inform the user
         *  that the system is waiting for their finger to be swiped IF the
         *  element is being shown. However, if the element is being hidden,
         *  the system is acknowledging that the user has started their swipe.
         *  It is normal to start a timer when "show please_swipe" is seen, and
         *  if the timer expires before "hide please_swipe" occurs, it's normal
         *  to send back a '#timeout' event.
         *  Other elements exist for the purpose of providing the user with
         *  usage feedback; these elements will be shown but not hidden. It is
         *  this activity's responsibility to decide when/if they should be
         *  taken down from the display. Often these types of feedback will be
         *  presented through an Android 'toast'. Examples of these feedback
         *  messages include: swipe_good, swipe_too_fast, swipe_too_slow,
         *  sensor_dirty, swipe_too_short, swipe_too_skewed,
         *  swipe_too_far_right, swipe_too_far_left.
         *  Generally it is believed that these element names explain the reason
         *  they will be shown, but there are some caveats to be aware of.
         *
         *  swipe_too_far_[right|left] may be inaccurate. That is, the
         *  left message may be shown when right is appropriate, and the reverse
         *  is true. The issue here is that the sensor can be mounted upside
         *  down, and the user could swipe in the opposite direction versus
         *  what the hardware design intends. As long as the swipe is in the
         *  center of the sensor, neither of those possibilities will have a
         *  negative effect on the operation, however they can cause the
         *  confusion. It is recommended that the same response be used for
         *  each of these conditions, and that the response merely tell the
         *  user two swipe in the center of the sensor.
         *
         *  Some of these messages will accompany another message immediately
         *  following: swipe_bad. It is tempting to simply ignore the swipe_bad
         *  message if the more direct message is being presented, however the
         *  swipe_bad message will also be sent (without other feedback) in the
         *  case of a good swipe failing to correctly verify against the
         *  database; the swipe_bad element is the only no-match notification.
         *
         *  Other elements that could be shown or hidden include C1 through C10
         *  and D1 through D10. These elements are only useful in the event
         *  that our UI includes a set of fingers and wishes to highlight
         *  certain fingers for some reason. If the UI does not include such a
         *  requirement, these elements can be ignored.
         *
         */
        if (command.equals("show")) {
            handleShow(args);
            return;
        }

        if (command.equals("hide")) {
            handleHide(args);
            return;
        }

        if (command.equals("settext")) {
            // This can generally be ignored. For UIs that get invoked on
            // behalf of multiple different applications, we expect to receive
            // a "settext appname <app name here>" command, in case we want to
            // tell the user which application is needing their fingerprint in
            // order to return a credential from the database.
            return;
        }

        if (DEBUG) Log.w(TAG, "Unhandled command: " + command);
    }

    private void toast(final String s)
    {
        runOnUiThread(new Runnable() {
            public void run()
            {
                mErrorPrompt.setText(s);
                mCountdownTimerToast = new CountDownTimer(1000, 1000) {
                    @Override
                    public void onFinish() {
                        mErrorPrompt.setText("");
                    }

                    @Override
                    public void onTick(long millisUntilFinished) {
                        // TODO Auto-generated method stub
                    }
                }.start();
            }
        });
    }

    public void onMusicChanged() {
        // refreshPlayingTitle();
    }
}