1 /*
2  * Copyright (C) 2007 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.example.android.apis.os;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 
22 import android.app.Activity;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.os.Vibrator;
26 import android.view.View;
27 import android.widget.TextView;
28 import com.example.android.apis.R;
29 
30 /**
31  * <h3>App that vibrates the vibrator with the Morse Code for a string.</h3>
32 
33 <p>This demonstrates the {@link android.os.Vibrator android.os.Vibrator} class.
34 
35 <h4>Demo</h4>
36 OS / Morse Code Vibrator
37 
38 <h4>Source files</h4>
39  * <table class="LinkTable">
40  *         <tr>
41  *             <td >src/com.example.android.apis/os/MorseCode.java</td>
42  *             <td >The Morse Code Vibrator</td>
43  *         </tr>
44  *         <tr>
45  *             <td >res/any/layout/morse_code.xml</td>
46  *             <td >Defines contents of the screen</td>
47  *         </tr>
48  * </table>
49  */
50 public class MorseCode extends Activity
51 {
52     /** Our text view */
53     private TextView mTextView;
54 
55     /**
56      * Initialization of the Activity after it is first created.  Must at least
57      * call {@link android.app.Activity#setContentView setContentView()} to
58      * describe what is to be displayed in the screen.
59      */
60     @Override
onCreate(Bundle savedInstanceState)61 	protected void onCreate(Bundle savedInstanceState)
62     {
63         // Be sure to call the super class.
64         super.onCreate(savedInstanceState);
65 
66         // See assets/res/any/layout/hello_world.xml for this
67         // view layout definition, which is being set here as
68         // the content of our screen.
69         setContentView(R.layout.morse_code);
70 
71         // Set the OnClickListener for the button so we see when it's pressed.
72         findViewById(R.id.button).setOnClickListener(mClickListener);
73 
74         // Save the text view so we don't have to look it up each time
75         mTextView = (TextView)findViewById(R.id.text);
76     }
77 
78     /** Called when the button is pushed */
79     View.OnClickListener mClickListener = new View.OnClickListener() {
80         public void onClick(View v) {
81             // Get the text out of the view
82             String text = mTextView.getText().toString();
83 
84             // convert it using the function defined above.  See the docs for
85             // android.os.Vibrator for more info about the format of this array
86             long[] pattern = MorseCodeConverter.pattern(text);
87 
88             // Start the vibration
89             Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
90             vibrator.vibrate(pattern, -1);
91         }
92     };
93 }
94