45. 45
PreferenceActivityクラス
/**
* The sample control preference activity handles the preferences for
* the sample control extension.
*/
public class SamplePreferenceActivity extends PreferenceActivity {
private static final int DIALOG_READ_ME = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preference);
// Handle read me
Preference preference = findPreference(getText(R.string.preference_key_read_me));
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
showDialog(DIALOG_READ_ME);
return true;
}
});
}
46. 46
PreferenceActivityクラス
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DIALOG_READ_ME:
dialog = createReadMeDialog();
break;
default:
Log.w(SampleExtensionService.LOG_TAG, "Not a valid dialog id: " + id);
break;
}
return dialog;
}
/**
* Create the Read me dialog
*
* @return the Dialog
*/
private Dialog createReadMeDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.preference_option_read_me_txt)
.setTitle(R.string.preference_option_read_me)
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton(android.R.string.ok, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
}
47. 47
RegistrationInformationクラス
/**
* Provides information needed during extension registration
*/
public class SampleRegistrationInformation extends RegistrationInformation {
final Context mContext;
/**
* Create control registration object
*
* @param context The context
*/
protected SampleRegistrationInformation(Context context) {
if (context == null) {
throw new IllegalArgumentException("context == null");
}
mContext = context;
}
@Override
public int getRequiredControlApiVersion() {
return 1;
}
@Override
public int getRequiredSensorApiVersion() {
return 0;
}
@Override
どのAPIを使うか
public int getRequiredNotificationApiVersion() {
return 0;
}
@Override
public int getRequiredWidgetApiVersion() {
return 0;
}
49. 49
BroadcastReceiverクラス
/**
* The extension receiver receives the extension intents and starts the
* extension service when it arrives.
*/
public class ExtensionReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d(SampleExtensionService.LOG_TAG, "onReceive: " + intent.getAction());
intent.setClass(context, SampleExtensionService.class);
context.startService(intent);
}
}
インテントにサービスとして呼び出す
クラスをセットしている
50. 50
ExtensionServiceクラス
public class SmartWatchTest00Service extends ExtensionService {
public static final String EXTENSION_KEY = "com.sonyericsson.extras.liveware.extension.samplecontrol.key";
public static final String LOG_TAG = "SmartWatchTest00";
public SmartWatchTest00Service() {
super(EXTENSION_KEY);
}
/**
* {@inheritDoc}
*
* @see android.app.Service#onCreate()
*/
@Override
public void onCreate() {
super.onCreate();
Log.d(SmartWatchTest00Service.LOG_TAG, "SmartWatchTest00: onCreate");
}
@Override
protected RegistrationInformation getRegistrationInformation() {
return new SmartWatchTest00RegistrationInformation(this);
}
LiveWareマネージャ登録クラス生成
51. 51
ExtensionServiceクラス
@Override
protected boolean keepRunningWhenConnected() {
return false;
}
@Override
public ControlExtension createControlExtension(String hostAppPackageName) {
final int controlSWWidth = SmartWatchTest00.getSupportedControlWidth(this);
final int controlSWHeight = SmartWatchTest00.getSupportedControlHeight(this);
for (DeviceInfo device : RegistrationAdapter.getHostApplication(this, hostAppPackageName)
.getDevices()) { SmartWatch
for (DisplayInfo display : device.getDisplays()) {
if (display.sizeEquals(controlSWWidth, controlSWHeight)) {
return new SmartWatchTest00(hostAppPackageName, this, new Handler());
の画面サイズ
}
}
}
などをチェック
}
throw new IllegalArgumentException("No control for: " + hostAppPackageName);
している
}
メインの処理クラスの生成
52. 52
メイン処理クラス
ControlExtensionクラス
public final void start()
public final void resume()
public final void pause()
public final void stop()
public final void destroy()
public void onDoAction(int requestCode, Bundle bundle)
public void onDestroy()
public void onStart()
public void onStop()
public void onPause()
public void onResume()
public void onError(final int code)
public void onKey(final int action, final int keyCode, final long timeStamp)
public void onTouch(final ControlTouchEvent event)
public void onSwipe(int direction)
protected void startRequest()
protected void stopRequest()
protected void showImage(final int resourceId)
protected void showBitmap(final Bitmap bitmap)
protected void showBitmap(final Bitmap bitmap, final int x, final int y)
protected void setScreenState(final int state)
protected void startVibrator(int onDuration, int offDuration, int repeats)
protected void stopVibrator()
protected void startLedPattern(int id, int color, int onDuration, int offDuration, int repeats)
protected void stopLedPattern(int id)
protected void clearDisplay()
protected void sendToHostApp(final Intent intent)
protected long getHostAppId()
protected boolean hasVibrator()
53. 53
メイン処理クラス
WidgetExtensionクラス
public final void startRefresh()
public final void stopRefresh()
public final void destroy()
public abstract void onStartRefresh()
public abstract void onStopRefresh()
public void onScheduledRefresh()
protected void scheduleRepeatingRefresh(long triggerAtTime, long interval, String extensionKey)
protected void scheduleRefresh(long triggerAtTime, String extensionKey)
protected void cancelScheduledRefresh(String extensionKey)
public void onDoAction(int requestCode, Bundle bundle)
public void onDestroy()
public void onTouch(final int type, final int x, final int y)
protected void sendImageToHostApp(final int resourceId)
protected void sendToHostApp(final Intent intent)
protected void showBitmap(final Bitmap bitmap)
54. 54
メイン処理クラス
画面を作る例
// Extract the last part of the host application package name.
String packageName = mHostAppPackageName
.substring(mHostAppPackageName.lastIndexOf(".") + 1); Bitmapを生成
// Create background bitmap for animation.
mBackground = Bitmap.createBitmap(width, height, BITMAP_CONFIG);
// Set default density to avoid scaling.
mBackground.setDensity(DisplayMetrics.DENSITY_DEFAULT);
LinearLayout root = new LinearLayout(mContext);
画面のレイアウトを生成
root.setLayoutParams(new LayoutParams(width, height));
LinearLayout sampleLayout = (LinearLayout)LinearLayout.inflate(mContext,
R.layout.sample_control, root);
((TextView)sampleLayout.findViewById(R.id.sample_control_text)).setText(packageName);
レイアウトに書き
sampleLayout.measure(width, height);
sampleLayout.layout(0, 0, sampleLayout.getMeasuredWidth(), 込み
sampleLayout.getMeasuredHeight());
Canvas canvas = new Canvas(mBackground);
sampleLayout.draw(canvas); レイアウトをBitmapに書き込む
showBitmap(mBackground);
画像を送信するインテントを発行している
55. 55
メイン処理クラス
画面を作る例
// Extract the last part of the host application package name.
String packageName = mHostAppPackageName
.substring(mHostAppPackageName.lastIndexOf(".") + 1); Bitmapを生成
// Create background bitmap for animation.
mBackground = Bitmap.createBitmap(width, height, BITMAP_CONFIG);
// Set default density to avoid scaling.
インテントで画像を送
mBackground.setDensity(DisplayMetrics.DENSITY_DEFAULT);
画面のレイアウトを生成
LinearLayout root = new LinearLayout(mContext);
root.setLayoutParams(new LayoutParams(width, height));
信すると知ったとき、
LinearLayout sampleLayout = (LinearLayout)LinearLayout.inflate(mContext,
R.layout.sample_control, root);
((TextView)sampleLayout.findViewById(R.id.sample_control_text)).setText(packageName);
レイアウトに書き
sampleLayout.measure(width, height);
sampleLayout.layout(0, 0, sampleLayout.getMeasuredWidth(), 込み
sampleLayout.getMeasuredHeight());
Canvas canvas = new Canvas(mBackground);
sampleLayout.draw(canvas); レイアウトをBitmapに書き込む
showBitmap(mBackground);
画像を送信するインテントを発行している
56. 56
メイン処理クラス
画面を作る例
// Extract the last part of the host application package name.
String packageName = mHostAppPackageName
.substring(mHostAppPackageName.lastIndexOf(".") + 1); Bitmapを生成
// Create background bitmap for animation.
mBackground = Bitmap.createBitmap(width, height, BITMAP_CONFIG);
// Set default density to avoid scaling.
ついに、Binderが 画面のレイアウトを生成
mBackground.setDensity(DisplayMetrics.DENSITY_DEFAULT);
LinearLayout root = new LinearLayout(mContext);
Bluetoothを超えたか!!
root.setLayoutParams(new LayoutParams(width, height));
LinearLayout sampleLayout = (LinearLayout)LinearLayout.inflate(mContext,
R.layout.sample_control, root);
((TextView)sampleLayout.findViewById(R.id.sample_control_text)).setText(packageName);
レイアウトに書き
sampleLayout.measure(width, height);
sampleLayout.layout(0, 0, sampleLayout.getMeasuredWidth(), 込み
sampleLayout.getMeasuredHeight());
Canvas canvas = new Canvas(mBackground);
sampleLayout.draw(canvas); レイアウトをBitmapに書き込む
showBitmap(mBackground);
画像を送信するインテントを発行している
57. 57
メイン処理クラス
画面を作る例
// Extract the last part of the host application package name.
String packageName = mHostAppPackageName
.substring(mHostAppPackageName.lastIndexOf(".") + 1); Bitmapを生成
// Create background bitmap for animation.
mBackground = Bitmap.createBitmap(width, height, BITMAP_CONFIG);
そんなことは 画面のレイアウトを生成
// Set default density to avoid scaling.
mBackground.setDensity(DisplayMetrics.DENSITY_DEFAULT);
LinearLayout root = new LinearLayout(mContext);
ありませんでした。
root.setLayoutParams(new LayoutParams(width, height));
LinearLayout sampleLayout = (LinearLayout)LinearLayout.inflate(mContext,
R.layout.sample_control, root);
((TextView)sampleLayout.findViewById(R.id.sample_control_text)).setText(packageName);
レイアウトに書き
sampleLayout.measure(width, height);
sampleLayout.layout(0, 0, sampleLayout.getMeasuredWidth(), 込み
sampleLayout.getMeasuredHeight());
Canvas canvas = new Canvas(mBackground);
sampleLayout.draw(canvas); レイアウトをBitmapに書き込む
showBitmap(mBackground);
画像を送信するインテントを発行している