• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /** Tag string for our debug logs */
53     private static final String TAG = "MorseCode";
54 
55     /** Our text view */
56     private TextView mTextView;
57 
58     ;
59 
60     /**
61      * Initialization of the Activity after it is first created.  Must at least
62      * call {@link android.app.Activity#setContentView setContentView()} to
63      * describe what is to be displayed in the screen.
64      */
65     @Override
onCreate(Bundle savedInstanceState)66 	protected void onCreate(Bundle savedInstanceState)
67     {
68         // Be sure to call the super class.
69         super.onCreate(savedInstanceState);
70 
71         // See assets/res/any/layout/hello_world.xml for this
72         // view layout definition, which is being set here as
73         // the content of our screen.
74         setContentView(R.layout.morse_code);
75 
76         // Set the OnClickListener for the button so we see when it's pressed.
77         findViewById(R.id.button).setOnClickListener(mClickListener);
78 
79         // Save the text view so we don't have to look it up each time
80         mTextView = (TextView)findViewById(R.id.text);
81     }
82 
83     /** Called when the button is pushed */
84     View.OnClickListener mClickListener = new View.OnClickListener() {
85         public void onClick(View v) {
86             // Get the text out of the view
87             String text = mTextView.getText().toString();
88 
89             // convert it using the function defined above.  See the docs for
90             // android.os.Vibrator for more info about the format of this array
91             long[] pattern = MorseCodeConverter.pattern(text);
92 
93             // Start the vibration
94             Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
95             vibrator.vibrate(pattern, -1);
96         }
97     };
98 }
99