1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.gesture.builder;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.os.Environment;
22 import android.view.View;
23 import android.view.MotionEvent;
24 import android.gesture.GestureOverlayView;
25 import android.gesture.Gesture;
26 import android.gesture.GestureLibrary;
27 import android.widget.TextView;
28 import android.widget.Toast;
29 
30 import java.io.File;
31 
32 public class CreateGestureActivity extends Activity {
33     private static final float LENGTH_THRESHOLD = 120.0f;
34 
35     private Gesture mGesture;
36     private View mDoneButton;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41 
42         setContentView(R.layout.create_gesture);
43 
44         mDoneButton = findViewById(R.id.done);
45 
46         GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);
47         overlay.addOnGestureListener(new GesturesProcessor());
48     }
49 
50     @Override
onSaveInstanceState(Bundle outState)51     protected void onSaveInstanceState(Bundle outState) {
52         super.onSaveInstanceState(outState);
53 
54         if (mGesture != null) {
55             outState.putParcelable("gesture", mGesture);
56         }
57     }
58 
59     @Override
onRestoreInstanceState(Bundle savedInstanceState)60     protected void onRestoreInstanceState(Bundle savedInstanceState) {
61         super.onRestoreInstanceState(savedInstanceState);
62 
63         mGesture = savedInstanceState.getParcelable("gesture");
64         if (mGesture != null) {
65             final GestureOverlayView overlay =
66                     (GestureOverlayView) findViewById(R.id.gestures_overlay);
67             overlay.post(new Runnable() {
68                 public void run() {
69                     overlay.setGesture(mGesture);
70                 }
71             });
72 
73             mDoneButton.setEnabled(true);
74         }
75     }
76 
77     @SuppressWarnings({"UnusedDeclaration"})
addGesture(View v)78     public void addGesture(View v) {
79         if (mGesture != null) {
80             final TextView input = (TextView) findViewById(R.id.gesture_name);
81             final CharSequence name = input.getText();
82             if (name.length() == 0) {
83                 input.setError(getString(R.string.error_missing_name));
84                 return;
85             }
86 
87             final GestureLibrary store = GestureBuilderActivity.getStore();
88             store.addGesture(name.toString(), mGesture);
89             store.save();
90 
91             setResult(RESULT_OK);
92 
93             final String path = new File(Environment.getExternalStorageDirectory(),
94                     "gestures").getAbsolutePath();
95             Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show();
96         } else {
97             setResult(RESULT_CANCELED);
98         }
99 
100         finish();
101 
102     }
103 
104     @SuppressWarnings({"UnusedDeclaration"})
cancelGesture(View v)105     public void cancelGesture(View v) {
106         setResult(RESULT_CANCELED);
107         finish();
108     }
109 
110     private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
onGestureStarted(GestureOverlayView overlay, MotionEvent event)111         public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
112             mDoneButton.setEnabled(false);
113             mGesture = null;
114         }
115 
onGesture(GestureOverlayView overlay, MotionEvent event)116         public void onGesture(GestureOverlayView overlay, MotionEvent event) {
117         }
118 
onGestureEnded(GestureOverlayView overlay, MotionEvent event)119         public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
120             mGesture = overlay.getGesture();
121             if (mGesture.getLength() < LENGTH_THRESHOLD) {
122                 overlay.clear(false);
123             }
124             mDoneButton.setEnabled(true);
125         }
126 
onGestureCancelled(GestureOverlayView overlay, MotionEvent event)127         public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
128         }
129     }
130 }
131