Chrome Custom Tabs を とりあえず数行で使う

スマホでブラウザを起動する, っての最近では面倒.

LINEとかメッセージでリンクを送ってくる人とか.

20150910-215123

さりげなく開かずにテキトーな会話を続けたりします.

考えてみると「なんかブラウザって重いしトロい」から.

そこで, 最近登場したのが「Chrome Custom Tabs」というしくみ.

Chrome Custom Tabs - Google Chrome

performance

表示までが非常に高速化されてるようにに見える.

実装しようとサンプルをダウンロードしてみる.

GoogleChrome/custom-tabs-client

アプリ内モジュールやヘルパーやプロセス間の通信など
依存関係があるのでわかりづらい.

とりあえずシンプルにURLを渡すだけでで起動してみたいだけなので以下で.

build.gradle

compile 'com.android.support:customtabs:23.0.1'

サンプル内モジュール customtabs 以下は不要.

コピペするクラス

CustomTabsHelper
KeepAliveService
CustomTabActivityHelper
WebViewFallback

shared というモジュールは作らずベタクラスとしてコピペ.
アプリ内モジュール依存はなしで.

起動

...
Uri uri = Uri.parse("http://google.com/");
CustomTabsIntent customTabsIntent =
    new CustomTabsIntent.Builder().build();
CustomTabActivityHelper
    .openCustomTab(this, customTabsIntent, uri, new WebViewFallback());
...

UIカスタマイズはしない. するならここで.

WebViewFallback#openUri()

    ...
    //Intent intent = new Intent(activity, WebViewActivity.class);
    //intent.putExtra(WebActivity.EXTRA_URL, uri.toString());
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    activity.startActivity(intent);
    ...

Chromeアプリ (stable/beta/dev) をインストールしてない場合は,
デフォルトブラウザなどに暗黙的に投げる.

で ?

起動はできる.
が, これだけでは恩恵にありつけない.

20150910-224352

サイト内検索などはできるようにはなるが
「なんかブラウザって重いしトロい」が解消せず.

以下の機能があるという.

Pre-warming of the Browser in the background, while avoiding stealing resources from the application.

Providing a likely URL in advance to the browser, which may perform speculative work, speeding up page load time.

これを使わないと意味ないな...

(つづく)


API-23 で「can’t find referenced class org.apache.http.*」

API-23 に上げるとサードパーティなライブラリ群に対して, こんな...

error

削除されているのですね.

API_Differences_between_22_and_23

API Differences between 22 and 23

各ライブラリが対応されるまではどうしたらよいのか.

...
android {
       useLibrary 'org.apache.http.legacy'
       ...
}
...

Apache HTTP connection with Android 6.0 (Marshmallow) - Stack Overflow

なんだか気持ち悪い.

動作の変更点 | Android Developers


rootなしで Android 端末をブラウザから操作できる Koushik の「Vysor」

すごいです.

画像が少し粗いものの, 操作は全くストレスがありません.

Chromeエクステンションです.

Vysor__Beta__-_Chrome_ウェブストア

Vysor (Beta) - Chrome ウェブストア

サイトも公開されています.

Vysor

Vysor

強烈なのがこの機能.

Vysor Share
Share your device, across the office, or across the globe. Screen and ADB access to remote devices. Simple as sending a link.

Chromeエクステの画面からクリップボードに取得できるURLを外部に送ると,

Vysor_and_Vysor

そのURLをクリックしたPCでネットを経由してそのAndroid端末を使えるようになる!!

Vysor

当然, どちらのPCのChromeにもエクステをインストールしておくことが必要です.

驚きました.

Vysor Share Remote screen and ADB access There's a few tools already out that…


onActivityResult() を透過的に Fragment に渡す

Activity内のFragmentに, 別Activityからの結果を渡す.

...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getFragmentManager().findFragmentById(R.id.your_fragment_id);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}
...

fragment いなかったら正しく fragment.setArguments(bundle); で.


Android で 定数 (int)で enum を使うことは 

こんな記事を見かけた.

Why don't you use enum ? // Speaker Deck

...
public class Status {
  public static final int OK = 0;
  public static final int ERROR = 1;
}

public void onSomethingAction(int status) {
  switch (status) {
    case Status.OK:
      Log.d(TAG, “Status : OK”);
      break;
    case Status.ERROR:
      Log.d(TAG, “Status : NG”);
      break;
  }
}
...

より,

...
public enum Status {
  OK,
  ERROR
}

public void onSomethingAction(Status status) {
  Log.d(TAG, “Status : “ + status.name());
}
...

のほうがいいよね, と.

なんとなくここら「enum」についての議論を
よく見かけたような気がしたのですこし調べてみる.

even in the most recent documents android suggests that it's not such a good idea to use enums in an android application. The reason why is because they use up more memory than a static constants variable.

Working with Enums in android - Stack Overflow

Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.

java - Why Android uses Ints instead of Enums - Stack Overflow

Android_Memories____Speaker_Deck

Android Memories // Speaker Deck

公式ドキュメントには,

Be aware of memory overhead
…
Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
…

Managing Your App's Memory | Android Developers

と書いてる.

他に,

Java enums are tremendously more expensive in code size than simple static final ints; classes and objects have not-insignificant overhead, so if you take a very class-heavy approach to your implementation you can end up with a significantly larger code size and RAM footprint for your app.

I really like this article on the importance of thinking about performance…

これに対して Jake Wharton さんも言及.

他には,

Should I strictly avoid using enums on Android?
No. Strictly means they are so bad, they should not be used at all. Possibly a performance issues might arise in an extreme situation like many many many (thousands or millions of) operations with enums (consecutive on the ui thread). Far more common are the network I/O operations that should strictly happen in a background thread. The most common usage of enums is probably some kind of type check - whether an object is this or that which is so fast you won't be able to notice a difference between a single comparison of enums and a comparison of integers.

java - Should I strictly avoid using enums on Android? - Stack Overflow

the original version of that document was just a bunch of prejudices. it's been rewritten to only contain facts backed up by actual benchmarks, and it's updated as the VM is updated. you can find the various benchmarks

Why was "Avoid Enums Where You Only Need Ints" removed from Android's performance tips? - Stack Overflow

結局,

オーバーヘッドは上がるが, まず最初はいうほど気にしなくてもよくね? 最近は. 見通しも良いし. 便利だし.

てな雰囲気でいいのかな. しらんけど.