summaryrefslogtreecommitdiffstats
path: root/tests/sketch/src/com/android/gesture/example/ContactListGestureOverlay.java
blob: 6767de632d8393dfbd3264183732c2faa477d32d (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
/*
 * Copyright (C) 2008-2009 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.gesture.example;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.gesture.LetterRecognizer;
import android.gesture.Prediction;

import java.util.ArrayList;

public class ContactListGestureOverlay extends Activity {
    private static final String LOG_TAG = "ContactListGestureOverlay";
    private static final String SORT_ORDER = People.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    private static final String[] CONTACTS_PROJECTION = new String[] {
            People._ID, // 0
            People.DISPLAY_NAME, // 1
    };

    private ContactAdapter mContactAdapter;

    private ListView mContactList;
    private LetterRecognizer mRecognizer;
    private GestureOverlayView mOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.overlaydemo);

        // create a letter recognizer
        mRecognizer = LetterRecognizer.getLetterRecognizer(this,
                LetterRecognizer.RECOGNIZER_LATIN_LOWERCASE);
        mOverlay = (GestureOverlayView) findViewById(R.id.overlay);

        // load the contact list
        mContactList = (ListView) findViewById(R.id.list);
        registerForContextMenu(mContactList);
        mContactList.setTextFilterEnabled(true);
        mContactList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                if (!mOverlay.isGesturing()) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, ContentUris.withAppendedId(
                            People.CONTENT_URI, id));
                    startActivity(intent);
                }
            }
        });

        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(People.CONTENT_URI, CONTACTS_PROJECTION, null, null,
                SORT_ORDER);
        ArrayList<ContactItem> list = new ArrayList<ContactItem>();
        while (cursor.moveToNext()) {
            list.add(new ContactItem(cursor.getLong(0), cursor.getString(1)));
        }
        mContactAdapter = new ContactAdapter(this, list);
        mContactList.setAdapter(mContactAdapter);

        mOverlay.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
        mOverlay.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                ArrayList<Prediction> predictions = mRecognizer.recognize(gesture);
                if (!predictions.isEmpty()) {
                    Log.v(LOG_TAG, "1st Prediction : " + predictions.get(0).name +
                            " @" + predictions.get(0).score);
                    Log.v(LOG_TAG, "2nd Prediction : " + predictions.get(1).name +
                            " @" + predictions.get(1).score);
                    Log.v(LOG_TAG, "3rd Prediction : " + predictions.get(2).name +
                            " @" + predictions.get(2).score);
                    int index = mContactAdapter.search(predictions.get(0).name);
                    if (index != -1) {
                        mContactList.setSelection(index);
                    }
                }
            }
        });
    }
}