Java Support(Javaへの対応)

Introduction(導入)

Android has hundreds of thousands of functions which apps can use. It’s not possible for Tasker to present all of those to the user, so Tasker allows the advanced user to directly call those Java functions and work with Java objects themselves.

It does not allow you to ‘write Java code’… but the combination of Tasker’s logic and flow control with direct access to the Android API is sufficient for most automation purposes.

This page assumes you have a basic familiarity with the Java concepts of objects and classes.

Developer information on the Android API is available from Google.

https://tasker.joaoapps.com/userguide/en/java.html

Androidには数十万ものアプリが利用できる関数が用意されています。Taskerがこの全てをユーザーに提供するのは無理なので、上級者向けにこれらJavaのオブジェクトを使って直接関数を呼び出すことを許しています。

Javaコードを直接書くことはできませんが、AndroidのAPIへの直接的なアクセスをTaskerのロジックやフロー制御と組み合わせる事で多くの自動化を効果的に行うことができます。

このページの内容はJaveのオブジェクトやクラスと言った概念の基本を理解している前提で書かれています。

開発者向けのAndroid APIに関する情報はGoogleから提供されています。

Example(用例)

  1. Variable Set, %service, wifi
  2. Java Function, wiman = CONTEXT.getSystemService( %service )
  3. Java Function, %enabled = wiman.isWifiEnabled()
  4. Java Function, wiman.setEnabled( true ), If %enabled eq false

This example task turns wifi on if it is not already enabled.

Action 2 demonstrates that Tasker variables can be used in Java function calls. wiman is a Java object resulting from the function call which is stored by Tasker for use in subsequent actions. CONTEXT is also such a variable but is built-in and always accessible to Java Function.

Action 3 demonstrates that results of Java Function can also be assigned to Tasker variables. Since all Tasker variables are strings, some conversion needs to take place depending on what type of object the Java function returns. In this case it’s a boolean, and so %enabled will be true or false.

Action 4 demonstrates taking a decision based on the result of previous Java function call.

https://tasker.joaoapps.com/userguide/en/java.html
  1. 変数を設定, %service, wifi
  2. Java関数, wiman = CONTEXT.getSystemService( %service )
  3. Java関数, %enabled = wiman.isWifiEnabled()
  4. Java関数, wiman.setEnabled( true ), If %enabled eq false

この例では、WiFiが利用可能では無い場合にWiFiをオフにしています。

2番目のアクションはJava関数の呼び出しの中でTaskerの変数が使用可能であることを示しています。wiman は関数呼び出しにより生成されるJavaのオブジェクトでその後のアクションで利用するためにTaskerによって保持されます。同様にCONTEXT はJava関数にいつでもアクセス出来る組み込みの変数です。

3番目のアクションではJava関数の結果がTaskerの変数にも格納可能であることを示しています。Taskerの変数は全て文字列として扱われるため、Java関数の結果としてオブジェクトが返される場合、その型に従って変換を行う必要があります。この例ではブール型の戻り値が返されるため %enabled には true または false が格納されます。

4番目のアクションでは先だってJava関数の呼び出しで得られた結果をもとに判断を行っています。

The Java Function Action(Java関数アクション)

Using The Action(アクションを使う)

  1. enter an object or class (to access static functions) into the first parameter. The magnifying glass icon will show a class selector for classes known in the latest Android API. Some may be coloured red, as not all classes are available on all devices. The coffee-cup icon allows quick selection of known Java objects The question mark icon will attempt to link to the relevant Android reference page for the object or class.
  2. click the magnifying class next to the Function parameter to select a function to execute appropriate to the object or class from step 1. In most cases, Tasker will be able to guess which class an object is, and hence which functions are available, if not, see casting below. Functions listed in red are private, meaning they can be used, but the author didn’t intend them to be.
  3. if the function returns a value, you can enter a Java object name to assign it to, or a Tasker variable, see below.
  4. enter any parameters required for the function, see below. The type of object the function expects for the parameter is displayed above the text entry field. The magnifying glass will list any fields associated with the current entry in the text box, where possible.
https://tasker.joaoapps.com/userguide/en/java.html
  1. オブジェクトまたはクラス(これを使って静的関数を利用します)を最初のパラメーターに入力します。虫眼鏡のアイコンをタップすると最新のAndroid APIで利用可能なクラスの一覧が表示されます。赤字で示されている物はデバイスによっては利用できない事を表しています。同様にコーヒーカップのアイコンでは利用可能なJavaオブジェクトを選択できます。クエスチョンマークのアイコンにはそれらオブジェクトやクラスに関するAndroidのリファレンスページへのリンクが割り当てられています。
  2. 関数パラメーターの右側の虫眼鏡のアイコンから1.で選択したクラスまたはオブジェクトにたいして実行する適切な関数の選択を行います。ほとんどの場合、Taskerはオブジェクトがどのクラスかを正しく推測して利用可能な関数を提示しますが、そうならない場合は後述のキャスティングの項をご覧下さい。赤字で表示されている関数はプライベート関数で、使用可能ではあるものの外部での使用を意図して作られたものでは無いことを示しています。
  3. 関数の戻り値がある場合、その値をアサインするためのJavaのオブジェクト名またはTaskerの変数を指定できます。戻り値の項をご覧下さい。
  4. 関数に必要なパラメーターを入力します。パラメーターの項をご覧下さい。関数が求めるオブジェクトの型は入力欄の上に表示されます。現在の入力項目がどのようなフィールドに関連づけられているかを表示可能な場合は、虫眼鏡のアイコンから一覧を確認できます。

Parameters(パラメーター)

If you don’t enter a value for a parameter, null will be used for that parameter when the function is called.

If you enter the name of a variable array, Tasker will attempt to convert the array values into the type of object (an array or collection) which the function expects.

Other Tasker variables will be replaced in the usual way.

Here can also be entered Java objects, or their fields, either built-in or created by previous calls to Java Function (e.g. wiman or arr[0].length)

Note: To use a string as a parameter you need to enclose it in double quotes or else Tasker assumes that the value you’re using is a new or existing Java object.

https://tasker.joaoapps.com/userguide/en/java.html

パラメーターを指定しない場合、関数呼び出しの際には null が渡されます。

配列変数の名前を指定した場合、Taskerはこれらの値を関数の要求に応じたオブジェクトの型(array または collection)に変換します。

それ以外のTasker変数は通常通り値に置き換えられます。

また、組み込みや関数の戻り値として得られたJavaオブジェクトやそのフィールド(wiman または arr[0].length など)も指定することが出来ます。

注記:文字列をパラメーターとして指定する場合にはダブルクォーテーションで囲む必要があります。でなければTaskerはそれらを新規または既存のJavaオブジェクトとして扱います。

Return Values(戻り値)

When a Java function returns a value, it can be placed in either a Tasker variable or a Java object (or ignored).

If it’s placed into a Tasker variable, it’s converted to a piece of text and the object itself is lost and can no longer be accessed. Note that if the Java object is an array or list, it will be assigned to multiple Tasker variables in the usual way e.g. %val1, %val2…

When the returned value is placed into a Java object, you can access the object at a later time in another Java Function and some other (see later) actions.

Note that return value classes are inferred from the function, so object names can refer to different classes at different times. It’s not recommended to reuse names in this way however!

https://tasker.joaoapps.com/userguide/en/java.html

Java関数から戻り値が返ると、その値はTasker変数またはJavaオブジェクトに格納されます(あるいは単に無視されます)。

Tasker変数に格納された場合、オブジェクトはテキストに変換されてしまうためオブジェクトとしてアクセスすることは出来なくなります。Javaオブジェクトが配列またはリストだった場合にはその値は複数のTasker変数として、通常 %val1 、%val2 、、、のように割り振られることに気を付けてください。

戻り値のクラスは関数の仕様に基づいて推測されるため、同一のオブジェクト名でもその時々で参照されるクラスが異なります。とはいえ、この方法でオブジェクト名を再利用することはお勧めできません。

Objects(オブジェクト)

Creating An Object(オブジェクトの作成)

New objects of most types can be created by filling in the class name, hitting the function selector and selecting a function called new.

It’s worth noting that many classes in the Android API have special static functions for getting a new object of that class called e.g. getInstance or similar.

Arrays (also multidimensional) can be created by adding [] to the class name (or e.g. [][]).

Here’s an example of creating a 3×5 string array:

  1. Java Function, arr = new String[][]( 3 )
  2. For, %rowno, 0:2
  3.    Java Function, arr[%rowno] = new String[]( 5 )

Creating an array is also possible natively via the newInstance function in the the class Array.

Array components can be accessed as in normal Java (arr[0][1]) wherever Java objects are supported.

https://tasker.joaoapps.com/userguide/en/java.html

ほとんどのタイプのオブジェクトは関数セレクタの new という関数を選択してクラス名を指定することで作成できます。

多くのAndroid APIのクラスにはそのクラスが呼ばれたときに新しいオブジェクトを生成するための特別な静的関数が有ることは特筆すべき事です。例:getInstance など

(多次元のものも含め)配列を作るにはクラス名に [] を続けて([][]のようにもできます)指定します。

ここに3×5の二次元配列の例を示します。

  1. Java関数, arr = new String[][]( 3 )
  2. For, %rowno, 0:2
  3.    Java関数, arr[%rowno] = new String[]( 5 )

配列の生成はクラス配列でnewInstance関数を用いて直接行うことも出来ます。

Javaオブジェクトが使用可能な場合は、通常のJavaの記法(arr[0][1])で配列を扱うことが出来ます。

Object Naming, Local and Global(オブジェクトの名付け方、ローカルとグローバル)

Object names can consist of any any combination of upper or lower case letters and underscore and, unlike Tasker variable names, may start with underscore. The first letter may not be upper-case, as this is a convention used to distinguish objects from classes.

Analogous to Tasker variables, Java objects are either local to the current task if their name is all lower case, or global (available to any other task) if there are any upper-case characters in the name. All-upper-case names represent final (fixed) global objects which cannot be modified.

There are three important things to remember about global Java objects:

  • it’s important to delete them once they are no longer needed, because they can take up a lot of memory.
  • unlike global Tasker variables, they are lost when Tasker is killed e.g. because the device was restarted
  • their names can only contain upper- or lower-case letters or underscore.
https://tasker.joaoapps.com/userguide/en/java.html

オブジェクトの名前は大文字・小文字およびアンダースコアの組み合わせやTaskerの変数名とは異なりアンダースコアから始まるものも認められています。最初の文字はオブジェクトとクラスを区別するために使われることになっているので大文字を使用することは出来ません。

Taskerの変数同様Javaオブジェクトの名前が全て小文字の場合は現在のタスクでのみ有効なローカルオブジェクトであり、大文字を含む名前の場合はグローバル(他のタスクからも参照可能)オブジェクトとなります。全て大文字の名前は最終(固定)的なグローバルオブジェクトでそれ以上の改変は出来ません。

以下にグローバルなJavaオブジェクトについて覚えておくべき3つのポイントを挙げます。

  • メモリーを多く消費してしまうため、必要がなくなったオブジェクトは削除する必要があります。
  • Taskerのグローバル変数とは異なり、端末が再起動するなどTaskerが終了されるとオブジェクトは失われます。
  • 名前に使えるのは大文字、小文字およびアンダースコアです。

Built-in Objects(組み込みオブジェクト)

  • Android Context (class Context)
    CONTEXT
    Many funtions in Android require a context object. In tasks running directly as a result of a scene element event, this is the Activity object which is displaying the scene, otherwise it’s Tasker’s Application context.
  • Image Buffer (class Bitmap)
    IBUFFER The object manipulated by functions in Tasker’s Image action category.
https://tasker.joaoapps.com/userguide/en/java.html
  • Android Context オブジェクト (Contextクラス)
    CONTEXT
    多くのAndroidの関数ではコンテキストオブジェクトを必要とします。シーン要素のイベントにより直接実行されるタスクにおいては、そのシーンを表示しているアクティビティーオブジェクトであり、それ以外はTaskerのアプリケーションコンテキストとなります。
  • Image BufferBitmapクラス)
    IBUFFER
    Taskerのイメージアクションカテゴリー内の関数で操作されるオブジェクトです。
    The object manipulated by functions in Tasker’s Image action category.

Assigning Values(値の割り当て)

When writing Java code, to make a name refer to the same object as another name, you would use something like:

	String a = "hello";
	String b = a;

Now both a and b refer to the same object.

To achieve that in Tasker, you use the special assignTo function after selecting the object to assign.

	Java Function, a, "hello", assign (or a = "hello".assign())
	Java Function, b, a, assign (or b = a.assign())
https://tasker.joaoapps.com/userguide/en/java.html

Javaコードを書く際、あるオブジェクトを別名で扱う場合以下の様にすることが多いと思います。

	String a = "hello";
	String b = a;

これで a と b は同じオブジェクトを参照する事になります。

これをTaskerで行うには、選択したオブジェクトに対して assignTo という割り当て用アクションを実行する必要があります。

	Java Function, a, "hello", assign (or a = "hello".assign())
	Java Function, b, a, assign (or b = a.assign())

Other Actions Supporting Objects(オブジェクトをサポートしているその他のアクション)

If(If 条件開始)

A Java object can be directly referenced in a condition. Null-value objects are replaced with text representation null.

Examples:

	If, arr[0][0] eq 45
	If, arr[0].length > 3 
	If, lightlevel Equals null

You cannot make function calls e.g. str.charAt( 5 )

https://tasker.joaoapps.com/userguide/en/java.html

Javaオブジェクトは分岐条件として使うことができます。Null値のオブジェクトは null を表す文字列に変換されます。

例:

	If, arr[0][0] eq 45
	If, arr[0].length > 3 
	If, lightlevel Equals null

str.charAt(5) の様な関数呼び出しはできません。

For(繰り返し開始)

The Value parameter in the For action can include Java object references as for If.

	For, %value, arr

Will repeat once for each value in the array arr. This will also work for string lists and simple objects (boolean etc)

https://tasker.joaoapps.com/userguide/en/java.html

Forアクションの「変数」パラメーターは「If 条件開始)」と同様Javaオブジェクトを含めることができます。

	For, %value, arr

これは arr配列の各要素の値に対して繰り返し処理を行います。これはまた文字列のリストや単純なオブジェクト(ブール値など)にも適用可能です。

Other Topics(その他のトピック)

Casting(キャスティング)

Casting in Tasker is used only to tell Tasker the type of a particular object. That can be useful so that e.g. Tasker can show functions which are appropriate to it.

In the example at the top of the page, the getSystemService function returns an Object:

	Java Function, wiman = CONTEXT.getSystemService( %service )

Since the object could be one of many kinds of managers, Tasker is not able to list the WifiManager functions for easy selection when creating the next Java Function action in the task.

You can tell Tasker the actual type by adding a cast in brackets before the name:

	Java Function, (WifiManager) wiman = CONTEXT.getSystemService( %service )
https://tasker.joaoapps.com/userguide/en/java.html

キャスティング(型変換)はTaskerでは単にオブジェクトの型をTaskerに示すだけのものです。これはTaskerがそのオブジェクトに適切な関数を提示するために役立っています。

このページの最初の例では、getSystemServiceアクションが戻り値としてオブジェクトを返します。

	Java Function, wiman = CONTEXT.getSystemService( %service )

このオブジェクトは様々な種類のマネージャーであり得るので、Taskerはタスク内の次のJava関数アクションの使用時に選択肢としてWifiManager関数を提示することができません。

関数名の前に特定の型へのキャストを追加することでTaskerに戻り値の型を知らせることができます。

	Java Function, (WifiManager) wiman = CONTEXT.getSystemService( %service )

Constants(定数)

Tasker support the usual naming conventions for Java constants.

  • L a long integer e.g. 300L
  • F a floating-point number e.g. 45.6D
  • D a double-length float e.g. 45.6D
  • double quotes indicate a string e.g. "hello", though in many cases Tasker will infer that a string was intended anyway
  • single quotes indicate a character e.g. 'x'

Tasker will attempt to convert numbers without affixes to a Java type in the following order: int, long, float, double.

https://tasker.joaoapps.com/userguide/en/java.html

TaskerはJavaの定数に対して通常の命名規則を適用します。

  • L は長整数を表します。例:300L
  • F は浮動小数点数を表します。例:45.6F
  • D は倍精度浮動小数点数を表します。例:45.6D
  • ダブルクォートは文字列を表します。例えば、”hello”の様にすることで文字列であることを明示できますが、Taskerは多くの場合において文字列であることを前提に処理を行います。
  • シングルクォートは文字を表します。例:’x’

Taskerは接辞の無い数値を int、long、float、doubleの順でJavaの数値型への変換を試みます。

Generic Classes(ジェネリッククラス)

Tasker only supports fully the following generic classes: ArrayList<String> ArrayList<View> ArrayList<Bundle> ArrayList<Integer> ArrayList<Long> ArrayList<Double> ArrayList<Float>

Create them by selecting their class in the class selector, clicking the function selector and clicking new.

Generic classes mixed with arrays cannot be handled by Tasker, though you can pass such objects around from function to function.

https://tasker.joaoapps.com/userguide/en/java.html

Taskerでは以下のジェネリック・クラスのみ完全にサポートしています:

ArrayList<String> ArrayList<View> ArrayList<Bundle> ArrayList<Integer> ArrayList<Long> ArrayList<Double> ArrayList<Float>

クラスセレクターにある関数セレクターから new をタップして作成します。

配列と混在するジェネリック・クラスはTaskerでは扱えません。このようなオブジェクトの関数から関数への受け渡しはできません。

Permissions(権限)

For some function calls, Android requires that the calling app have declared a permission otherwise the call will fail. This means that a Java Function call will fail if the permission is not one of the ones pre-declared by Tasker.

Unfortunately, Android does not allow permissions to be added dynamically, so if you wish to use a function requiring a permission that Tasker does not already have, the only option is to generate a child app to run the function (see App Creation). In the child configuration screen you can add any permissions which your Java Function call needs to the child app.

https://tasker.joaoapps.com/userguide/en/java.html

関数呼び出しのいくつかではAndroidが呼び出し側アプリにパーミッション(権限)の宣言を必要としているため、宣言されていない場合は失敗します。これはTaskerによって予め宣言されているパーミッション以外の権限を必要とする場合にはJava関数の呼び出しは失敗することを意味します。

不幸にしてAndroidは動的なパーミッションの追加を認めていません。したがって、Taskerが既に宣言している以外のパーミッションを必要とする関数を使用する唯一の方法は、関数の実行のために子アプリを作成することです(アプリの作成をご覧下さい)。子アプリの設定画面からJava関数の呼び出しに必要なパーミッションを設定出来ます。

Service Thread(サービススレッド)

Java code is executed with a non-UI thread by a service.

Some implications are:

  • things which require an activity will not work e.g. showing a dialog
  • sending intents will in some cases require the flag Intent.FLAG_FROM_BACKGROUND and possibly also Intent.FLAG_ACTIVITY_NEW_TASK
https://tasker.joaoapps.com/userguide/en/java.html

JavaコードはサービスによってUIを持たないスレッドで実行されます。

これにより

  • ダイアログの表示などアクティビティーを必要とする動作は行えません。
  • インテントの送信は場合によって Intent.FLAG_FROM_BACKGROUND の他 FLAG_ACTIVITI_NEW_TASK などのフラグを必要とします。

Static Fields(静的フィールド)

Static fields (e.g. ContentResolver.EXTRA_SIZE) are not currently supported by Tasker.

A workaround is to use reflection to get (or set) the value:

	res = CONTEXT.getContentResolver();
cls = res.getClass();
fld = cls.getField( EXTRA_SIZE );
%val = fld.get( null );
https://tasker.joaoapps.com/userguide/en/java.html

静的フィールド(例えば、ContentResolver.EXTRA_SIZE)は現在Taskerではサポートされていません。

解決策として値の取得(または設定)にリフレクションを使う方法があります。

	res = CONTEXT.getContentResolver();
	cls = res.getClass();
	fld = cls.getField( EXTRA_SIZE );
	%val = fld.get( null );