1. Trang chủ
  2. » Công Nghệ Thông Tin

extremetech Hacking BlackBerry phần 9 docx

31 143 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Advanced BlackBerry Hacks
Trường học BlackBerry University
Chuyên ngành Mobile Programming
Thể loại 教学文档
Năm xuất bản 2006
Thành phố Unknown
Định dạng
Số trang 31
Dung lượng 407,17 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

private static final short duration1sec = 1000; This makes it easier to see what your code is doing.. Instead of specifying a G note that plays for 1 second as {784, 1000}, I think you w

Trang 1

{public void run(){

// Play a B note}

Trang 2

// Play a Gb note}

};

You’ll note that in the run()code for each menu handler, I have not yet added the actual code

to play the corresponding note For that, you need to define a “tune” structure for each note, aswell as call the Alert.startAudio()function to play the note

Playing Notes Using Alert.startAudio

In order to have your PianoBerry menu items actually play their corresponding note, youneed to call the BlackBerry function Alert.startAudio().Alert.startAudiotakes twoparameters, one that represents the “tune” to be played and another that represents the percentvolume you want to play the tune at The tune parameter is actually an array of integer pairsthat use the following format:{frequency, duration} Each pair thus plays a note of agiven frequency for the length of time you specify In this pair of integers, the frequency isexpressed in MHz, while the duration is expressed as milliseconds

Although the tune parameter is an array and can contain an actual sequence of notes to be played,for the purposes of PianoBerry you are interested in playing only a single note at a time Sowhat you need to do is to figure out the frequency value for each of the 12 notes in your scale,and then create pairs of integers that define the note and duration

Earlier in the chapter you determined frequency values for each of the notes in your scale Yourscale starts arbitrarily at A with a value of 440, and proceeds higher up the scale until you reachthe highest note, Ab, which has a defined value of 831 I suppose you could be lazy and simplytype in the frequency number wherever you call Alert.startAudio(), but it is cleaner andmakes your code easier to read if instead you create named variables that represent the notesyou want to play

So, to express these values properly in the PianoBerry Java code, you simply define a set ofnamed short integers as data members within the PianoBerryScreenclass, as follows:

// these represent the frequencies for your scaleprivate static final short A = 440; // 440.00

Trang 3

private static final short Bb = 466; // 466.16private static final short B = 494; // 493.88private static final short C = 523; // 523.25private static final short Db = 554; // 554.36private static final short D = 587; // 587.32private static final short Eb = 622; // 622.25private static final short E = 659; // 659.25private static final short F = 698; // 698.45private static final short Gb = 740; // 739.98private static final short G = 784; // 783.99private static final short Ab = 831; // 830.60

Once you’ve done this, anywhere you want to play a G note, you simply reference it by thevariable name Ginstead of having to always remember that the frequency of G is 784.Similarly, rather than hard-coding a duration value for the note everywhere you call

startAudio, you can predefine another data member called duration1secthat will have

a value of 1000(milliseconds)

private static final short duration1sec = 1000;

This makes it easier to see what your code is doing Instead of specifying a G note that plays for

1 second as {784, 1000}, I think you will agree that it is nicer to write {G, duration1sec}.You can also pre-define a volume level you want to use for the same reason:

private static final int VOLUME = 100; // Percentage volume

Now, because startAudio()requires that you pass both the frequency and the duration as aninteger array, the final little code-cleanliness thing you want to do is pre-define a set of namedtwo-integer arrays, which you can then directly pass into the startAudio()call wheneveryou want to play a note So, you write up 12 additional class data members, with names such as

TUNEA,TUNEBb,TUNEC, and so on These two-integer arrays are coded as follows:

// pre-defined tune arrays for use with the startAudio function private static final short[] TUNEA = new short[] {A, duration1sec}; private static final short[] TUNEBb = new short[] {Bb, duration1sec}; private static final short[] TUNEB = new short[] {B, duration1sec}; private static final short[] TUNEC = new short[] {C, duration1sec}; private static final short[] TUNEDb = new short[] {Db, duration1sec}; private static final short[] TUNED = new short[] {D, duration1sec}; private static final short[] TUNEEb = new short[] {Eb, duration1sec}; private static final short[] TUNEE = new short[] {E, duration1sec}; private static final short[] TUNEF = new short[] {F, duration1sec}; private static final short[] TUNEGb = new short[] {Gb, duration1sec}; private static final short[] TUNEG = new short[] {G, duration1sec}; private static final short[] TUNEAb = new short[] {Ab, duration1sec};

Now you’ve set yourself up perfectly so that whenever you need to play a tune, you just call

Alert.startAudio()as follows:

Alert.startAudio(TUNEAb, VOLUME);

Trang 4

You can now fill in your menu handlers with calls to startAudio, as follows:

private MenuItem AItem = new MenuItem(“A”, 0, 0) {

public void run() {

// Play an A note for 1 second

Alert.startAudio (TUNEA, VOLUME) }

};

The remaining 11 menu handlers for the notes in your scale are coded in a similar fashion

At this point, if you’ve entered all the pieces of code correctly, you can actually test outPianoBerry using the menu system on either the BlackBerry Simulator or an actual device

When you run PianoBerry, you will see a blank screen with a simple title, “PianoBerry.” Toplay a note, click the trackwheel menu and choose one of the menu items corresponding to anote, as shown in Figure 13-1

F IGURE 13-1: PianoBerry’s main menu lists the notes you can play.

Drum roll, please When you click the menu item for a note in the scale, you should hear thenote through either your computer speaker or the BlackBerry speaker That first note sounds sosweet!

Capturing Keypad Events

At this point the hard work is done You’ve coaxed your BlackBerry into playing 12 differentnotes from a piano scale The only problem is that every time you want to play a note you have

to click the menu to choose a note That’s kind of tedious, right? If this was how a real pianoworked, Beethoven might very well have decided to take up gardening! Obviously you need tohave a more intuitive way to play notes if you want to convince anyone to compose a pianosonata on their BlackBerry

Trang 5

A quick glance at a BlackBerry yields the clear solution: PianoBerry should assign each note to

a key on the BlackBerry keypad, so that when you press a key, a note plays, just like the keys

on a real piano To be sure, the BlackBerry QWERTY keypad layout is not a perfect match for

a piano keyboard, but with a little creative thinking in terms of mapping keys to notes, youshould be able to make it work

You now have a plan, but you also have a challenge: How does a BlackBerry applicationbecome aware that the user pressed a key on the keypad? Certainly it must be possible to do sobecause numerous BlackBerry applications let you type text using the keypad and even assignspecial functions to different keys

In the development projects you’ve covered thus far, you have not needed to catch keystrokes,but you have had to trap a different kind of hardware event, the trackwheel In each of thechapter examples, you’ve generally implemented the TrackwheelListenerinterface in order

to receive trackwheelClickevents and display a menu What if there were a similar nism for trapping keypad events?

mecha-Sure enough, there is The KeyListenerinterface is typically added to a MainScreenclassthat wants to “listen” in on keypad events With TrackwheelListener, you needed to addhandlers for trackwheelClick,trackwheelRoll, and trackwheelUnclick These han-dlers acted as an override to the built-in MainScreentrackwheel handling Similarly, with

KeyListenerthe member functions you can override in your MainScreenclass are keyChar,

keyDown,keyUp,keyStatus, and keyRepeat.keyDown,keyUp,keyStatus, and keyRepeat

let you have pretty granular control over specific key actions, but keyCharis the one you want

to focus on now

The keyChar()function notifies you when BlackBerry has determined that the user hasformed a character with a single key press or a valid combination of a key with modifier keyssuch as ALT, SHIFT, or CAPS LOCK In contrast to keyChar(),keyDownand keyUparelower-level handlers that just detect when a key is pressed or released The job of figuring out

that the user meant to type a capital A is up to you With keyChar(), the handler does all theinterpretation for you, so you don’t have to worry about keyUp()or keyDown()

The keyChar()function looks like this:

public boolean keyChar(char key, int status, int time)

Although keyCharpasses your class three parameters, the main piece of information is the

keyparameter The others are simply extra information in case you want to know if any fier keys were pressed or what the exact time was when the key was pressed The keyparame-ter represents the actual character that was generated by the keyboard event

modi-On other kinds of computers, figuring out what character was entered often means you have tolearn ASCII codes and hexadecimal numbers BlackBerry has simplified things for programmers

by offering a special Java class called Charactersthat contains a named value representing

every possible character that can be generated by the keypad So a capital A is represented by

the constant Characters.LATIN_CAPITAL_LETTER_A, a lowercase c is represented by the

constant Characters.LATIN_SMALL_LETTER_C, and so on

Trang 6

Armed with this information you can now test for specific characters that were pressed like so:

public boolean keyChar(char key, int status, int time){

switch (key){

case Characters.LATIN_CAPITAL_LETTER_A:

{// a capital “A” was pressed - do somethingreturn true;

}// add other case statements here for each character// you want to handle

}return false;

}

Mapping BlackBerry Keys to Piano Keys

Now that you know how to capture keypad events and trap when specific characters are entered,you have an interesting decision to make Which BlackBerry keys should play which music notes?

Given the QWERTY keyboard layout and how different it is from a standard piano keyboard,plus the lack of anything close to the white key/black key piano keyboard arrangement, howyou map BlackBerry keys to piano notes is really kind of arbitrary There is no “right” way to do

it One possibility to consider is to assign each full note to its corresponding letter on the pad (an A note would play if you pressed the A key, and so on) But what about flatted notessuch as Ab? Conceivably that could be handled by using a modifier key such as ALT or SHIFT,but ultimately that seems awkward Besides, the whole point of this exercise is to make it easier

key-to play notes than it is by using the trackwheel menu; adding special keystrokes is at odds withthat goal

A simpler solution is to take the 12 keys that run across the top of the BlackBerry keypad andmap them sequentially, starting at the low A and ending in the high Ab at the top of the scale

To accomplish this, you can use the mapping shown in Table 13-2

Table 13-2 PianoBerry Key Mappings

BlackBerry Key Piano Note

Trang 7

public boolean keyChar(char key, int status, int time){

super.keyChar(key, status, time);

switch (key){

case Characters.LATIN_CAPITAL_LETTER_A:

case Characters.LATIN_SMALL_LETTER_A:

{Alert.startAudio(TUNEA, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_Q:

case Characters.LATIN_SMALL_LETTER_Q:

{Alert.startAudio(TUNEBb, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_W:

case Characters.LATIN_SMALL_LETTER_W:

{Alert.startAudio(TUNEB, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_E:

case Characters.LATIN_SMALL_LETTER_E:

{Alert.startAudio(TUNEC, VOLUME);

return true;

}

Trang 8

case Characters.LATIN_CAPITAL_LETTER_R:

case Characters.LATIN_SMALL_LETTER_R:

{Alert.startAudio(TUNEDb, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_T:

case Characters.LATIN_SMALL_LETTER_T:

{Alert.startAudio(TUNED, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_Y:

case Characters.LATIN_SMALL_LETTER_Y:

{Alert.startAudio(TUNEEb, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_U:

case Characters.LATIN_SMALL_LETTER_U:

{Alert.startAudio(TUNEE, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_I:

case Characters.LATIN_SMALL_LETTER_I:

{Alert.startAudio(TUNEF, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_O:

case Characters.LATIN_SMALL_LETTER_O:

{Alert.startAudio(TUNEGb, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_P:

case Characters.LATIN_SMALL_LETTER_P:

{Alert.startAudio(TUNEG, VOLUME);

return true;

}case Characters.LATIN_CAPITAL_LETTER_L:

case Characters.LATIN_SMALL_LETTER_L:

{Alert.startAudio(TUNEAb, VOLUME);

return true;

}return false;

}

The preceding code is pretty straightforward It traps each keypad letter in your layout, and itplays the appropriate music note using Alert.startAudio()

Trang 9

PianoBerry: Putting It All Together

You did it! You now have a working program that can play 12 notes on the BlackBerry keypad.The keyboard layout is a bit strange, and the audio quality is a far cry from a baby grand, but itworks nonetheless

Here is the complete source code listing for PianoBerry (The full source code and the JDEproject are also available on the Wiley website at www.wiley.com/go/extremetech.) If youbuild this source in the BlackBerry JDE, you will wind up with a PianoBerry program that can

be installed to your BlackBerry through the BlackBerry Desktop Application Loader feature

pushScreen(new PianoBerryScreen());

} } class PianoBerryScreen extends MainScreen implements KeyListener, i

TrackwheelListener {

// these represent the frequencies for your scale private static final short A = 440; // 440.00 private static final short Bb = 466; // 466.16 private static final short B = 494; // 493.88 private static final short C = 523; // 523.25 private static final short Db = 554; // 554.36 private static final short D = 587; // 587.32 private static final short Eb = 622; // 622.25 private static final short E = 659; // 659.25 private static final short F = 698; // 698.45 private static final short Gb = 740; // 739.98 private static final short G = 784; // 783.99 private static final short Ab = 831; // 830.60 // how long each note will play for (in milliseconds) private static final short duration1sec = 1000;

Trang 10

// pre-defined tune arrays for use with the startAudio function private static final short[] TUNEA = new short[] {A, duration1sec};

private static final short[] TUNEBb = new short[] {Bb, duration1sec};

private static final short[] TUNEB = new short[] {B, duration1sec};

private static final short[] TUNEC = new short[] {C, duration1sec};

private static final short[] TUNEDb = new short[] {Db, duration1sec};

private static final short[] TUNED = new short[] {D, duration1sec};

private static final short[] TUNEEb = new short[] {Eb, duration1sec};

private static final short[] TUNEE = new short[] {E, duration1sec};

private static final short[] TUNEF = new short[] {F, duration1sec};

private static final short[] TUNEGb = new short[] {Gb, duration1sec};

private static final short[] TUNEG = new short[] {G, duration1sec};

private static final short[] TUNEAb = new short[] {Ab, duration1sec};

private static final int VOLUME = 100; // Percentage volume.

// some menu items you will add to your menu private MenuItem _closeItem = new MenuItem(“Close”, 0, 0) {

public void run() {

onClose();

} };

// this menu is useful to show what audio capabilities i

are supported on a device private MenuItem AudioSupportItem = new MenuItem(“Audio Support”, 0, 0) {

public void run() {

boolean bAudio = Alert.isAudioSupported();

boolean bBuzzer = Alert.isBuzzerSupported();

boolean bVibrate = Alert.isVibrateSupported();

boolean bMIDI = Alert.isMIDISupported();

boolean bADPCM = Alert.isADPCMSupported();

if (bAudio) {

Dialog.alert(“Audio supported”);

}

if (bMIDI) {

Dialog.alert(“MIDI supported”);

}

if (bADPCM) {

Dialog.alert(“ADPCM supported”);

} // shows how to set the volume Alert.setVolume (100);

} };

Trang 11

// these are all menu items just as alternative means for selecting a note.

private MenuItem AItem = new MenuItem(“A”, 0, 0) {

public void run() {

Alert.startAudio(TUNEA, VOLUME);

} };

private MenuItem BbItem = new MenuItem(“Bb”, 1, 0) {

public void run() {

Alert.startAudio(TUNEBb, VOLUME);

} };

private MenuItem BItem = new MenuItem(“B”, 2, 0) {

public void run() {

Alert.startAudio(TUNEB, VOLUME);

} };

private MenuItem CItem = new MenuItem(“C”, 2, 0) {

public void run() {

Alert.startAudio(TUNEC, VOLUME);

} };

private MenuItem DbItem = new MenuItem(“Db”, 2, 0) {

public void run() {

Alert.startAudio(TUNEDb, VOLUME);

} };

private MenuItem DItem = new MenuItem(“D”, 2, 0) {

public void run() {

Alert.startAudio(TUNED, VOLUME);

} };

private MenuItem EbItem = new MenuItem(“Eb”, 2, 0) {

public void run() {

Alert.startAudio(TUNEEb, VOLUME);

}

Trang 12

private MenuItem FItem = new MenuItem(“F”, 2, 0) {

public void run() {

Alert.startAudio(TUNEF, VOLUME);

} };

private MenuItem GbItem = new MenuItem(“Gb”, 2, 0) {

public void run() {

Alert.startAudio(TUNEGb, VOLUME);

} };

private MenuItem GItem = new MenuItem(“G”, 2, 0) {

public void run() {

Alert.startAudio(TUNEG, VOLUME);

} };

private MenuItem AbItem = new MenuItem(“Ab”, 2, 0) {

public void run() {

Alert.startAudio(TUNEAb, VOLUME);

} };

public PianoBerryScreen() {

super();

LabelField applicationTitle = new LabelField(“Piano Berry”);

setTitle(applicationTitle);

} public boolean onClose() {

Dialog.alert(“What? Leaving so soon?”);

System.exit(0);

return true;

}

Trang 13

// overrides MainScreen’s standard keypad handling by i

associating the top row of keys to the notes of the scale // Starting from the left side of the keyboard, the mappings are: // Key Note

// A A // Q Bb // W B // E C // R Db // T D // Y Eb // U E // I F // O Gb // P G // L A public boolean keyChar(char key, int status, int time) {

super.keyChar(key, status, time);

switch (key) {

case Characters.LATIN_CAPITAL_LETTER_A:

case Characters.LATIN_SMALL_LETTER_A:

{ Alert.startAudio(TUNEA, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_Q:

case Characters.LATIN_SMALL_LETTER_Q:

{ Alert.startAudio(TUNEBb, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_W:

case Characters.LATIN_SMALL_LETTER_W:

{ Alert.startAudio(TUNEB, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_E:

case Characters.LATIN_SMALL_LETTER_E:

{ Alert.startAudio(TUNEC, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_R:

case Characters.LATIN_SMALL_LETTER_R:

{ Alert.startAudio(TUNEDb, VOLUME);

return true;

Trang 14

} case Characters.LATIN_CAPITAL_LETTER_T:

case Characters.LATIN_SMALL_LETTER_T:

{ Alert.startAudio(TUNED, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_Y:

case Characters.LATIN_SMALL_LETTER_Y:

{ Alert.startAudio(TUNEEb, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_U:

case Characters.LATIN_SMALL_LETTER_U:

{ Alert.startAudio(TUNEE, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_I:

case Characters.LATIN_SMALL_LETTER_I:

{ Alert.startAudio(TUNEF, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_O:

case Characters.LATIN_SMALL_LETTER_O:

{ Alert.startAudio(TUNEGb, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_P:

case Characters.LATIN_SMALL_LETTER_P:

{ Alert.startAudio(TUNEG, VOLUME);

return true;

} case Characters.LATIN_CAPITAL_LETTER_L:

case Characters.LATIN_SMALL_LETTER_L:

{ Alert.startAudio(TUNEAb, VOLUME);

return true;

} // intercept the ESC key - exit the app on its receipt case Characters.ESCAPE:

{

if (Dialog.YES == Dialog.ask(Dialog.D_YES_NO, i

“Are you sure you want to exit?”, Dialog.YES ))

{ OnClose ();

Trang 15

} else { return true;

} break;

} } return false;

} public boolean keyDown(int keycode, int time) {

return false;

} public boolean keyRepeat(int keycode, int time) {

return false;

} public boolean keyStatus(int keycode, int time) {

return false;

} public boolean keyUp(int keycode, int time) {

return false;

} // this overrides makeMenu in UIApplication, and gives you i

full control over your menu protected void makeMenu(Menu menu, int instance) {

// this attempts to see if there are any context menus added, i

and makes sure that they are retained

Field focus = i

UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();

if(focus != null) { ContextMenu contextMenu = focus.getContextMenu();

if( !contextMenu.isEmpty()) { menu.add(contextMenu);

menu.addSeparator();

} } // here you add your own items menu.add(AudioSupportItem);

Ngày đăng: 08/08/2014, 21:23

TỪ KHÓA LIÊN QUAN