|
1 /******************************************************************************* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. This program and the accompanying materials |
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
|
5 * which accompanies this distribution, and is available at |
|
6 * http://www.eclipse.org/legal/epl-v10.html |
|
7 * |
|
8 * Contributors: |
|
9 * Nokia Corporation - initial implementation |
|
10 *******************************************************************************/ |
|
11 |
|
12 package org.eclipse.swt.internal.extension; |
|
13 |
|
14 import org.eclipse.swt.internal.qt.OS; |
|
15 import org.eclipse.swt.widgets.Display; |
|
16 import org.eclipse.swt.widgets.Internal_PackageSupport; |
|
17 |
|
18 public final class DisplayExtension extends Display { |
|
19 |
|
20 private static final class VibraRunnable implements Runnable { |
|
21 private final int duration; |
|
22 private final Display display; |
|
23 boolean result; |
|
24 private VibraRunnable(int duration, Display display) { |
|
25 this.duration = duration; |
|
26 this.display = display; |
|
27 } |
|
28 |
|
29 public void run() { |
|
30 result= OS.MobileDevice_vibration( |
|
31 Internal_PackageSupport.initializeMobileDevice(display),duration); |
|
32 |
|
33 } |
|
34 } |
|
35 private static final class FlashBacklightsRunnable implements Runnable { |
|
36 private final Display display; |
|
37 private final int duration; |
|
38 boolean result; |
|
39 |
|
40 private FlashBacklightsRunnable(Display display, int duration) { |
|
41 this.display = display; |
|
42 this.duration = duration; |
|
43 } |
|
44 |
|
45 public void run() { |
|
46 result = OS.MobileDevice_flashLights( |
|
47 Internal_PackageSupport.initializeMobileDevice( |
|
48 display),duration); |
|
49 } |
|
50 } |
|
51 |
|
52 /* Image types, same values as MIDP */ |
|
53 public static final int LIST_ELEMENT = 1; |
|
54 public static final int CHOICE_GROUP_ELEMENT = 2; |
|
55 public static final int ALERT = 3; |
|
56 |
|
57 public DisplayExtension() { |
|
58 super(); |
|
59 } |
|
60 |
|
61 /** |
|
62 * Determine the best width for the given image type. |
|
63 * @param imageType |
|
64 * @return Best height or -1 if invalid image type given. |
|
65 */ |
|
66 public static int getBestImageWidth(int imageType) { |
|
67 return getBestImageSize(imageType); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Determine the best height for the given image type. |
|
72 * @param imageType |
|
73 * @return Best height or -1 if invalid image type given. |
|
74 */ |
|
75 public static int getBestImageHeight(int imageType) { |
|
76 return getBestImageSize(imageType); |
|
77 } |
|
78 |
|
79 /** |
|
80 * Gets the default display instance, but does not create one if it doesn't exist. |
|
81 * @return Default display if one has been initialized otherwise null |
|
82 */ |
|
83 public static Display getDisplayInstance() { |
|
84 return Internal_PackageSupport.getDisplayInstance(); |
|
85 } |
|
86 /** |
|
87 * Flashes devices backlight. |
|
88 * |
|
89 * @param duration Duration in milliseconds. |
|
90 * @return false if no flashing happened because MIDlet was in background or |
|
91 * feature is not supported. |
|
92 */ |
|
93 public static boolean flashLights( final int duration ){ |
|
94 final Display display = getDisplayInstance(); |
|
95 if (display == null || display.isDisposed() ) return false; |
|
96 FlashBacklightsRunnable runnable = new FlashBacklightsRunnable(display, duration); |
|
97 display.syncExec(runnable); |
|
98 return runnable.result; |
|
99 } |
|
100 /** |
|
101 * Vibrates device |
|
102 * |
|
103 * @param duration Duration in milliseconds. |
|
104 * @return false if no vibration happened because MIDlet was in background |
|
105 * or feature is not supported. |
|
106 */ |
|
107 public static boolean startVibra(final int duration) { |
|
108 final Display display = getDisplayInstance(); |
|
109 if (display == null || display.isDisposed() ) return false; |
|
110 VibraRunnable runnable = new VibraRunnable(duration, display); |
|
111 display.syncExec(runnable); |
|
112 return runnable.result; |
|
113 } |
|
114 /** |
|
115 * |
|
116 * @param imageType |
|
117 * @return |
|
118 */ |
|
119 private static int getBestImageSize(int imageType) { |
|
120 switch (imageType) { |
|
121 case LIST_ELEMENT: |
|
122 case CHOICE_GROUP_ELEMENT: |
|
123 return Style.pixelMetric(Style.QSTYLE_PM_SMALLICONSIZE); |
|
124 case ALERT: |
|
125 return Style.pixelMetric(Style.QSTYLE_PM_MESSAGEBOXICONSIZE); |
|
126 default: |
|
127 return -1; |
|
128 } |
|
129 } |
|
130 |
|
131 |
|
132 } |