본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] App Shortcuts Tutorial

by 돼지왕 왕돼지 2018. 2. 25.
반응형

[android] App Shortcuts Tutorial


https://catinean.com/2016/10/20/exploring-android-nougat-7-1-app-shortcuts/

7.1, activity backstack, activity stack, Android N, android.app.shortcuts, app shortcut, charsequence, Dimm, Dynamic, dynamic shortcut, enabled, ForegroundColorSpan, getSystemService, intent array, Label, launcher, long press, LongLabel, MainActivity, manifest, meta-data, nougat, now launcher, PIN, Pixel Launcher, rank, Resource, Runtime, setDynamicShortcuts, setIcon, setIntent, setIntents, setLongLabel, setRank, setShortLabel, setSpan, shortCut, shortcut max count, shortcut xml, shortcut 순서, shortcutDisabledMessage, ShortcutInfo, ShortcutInfo.Builder, shortcutLongLabel, ShortcutManager, shortcuts, shortcutShortLabel, spannable, spannablestringbuilder, static, static shortcut, stylish, tutorial, updateShortcuts, xml, [android] App Shortcuts Tutorial, 고정, 바로가기, 바로가기 옵션, 순서, 하단


-

7.1, activity backstack, activity stack, Android N, android.app.shortcuts, app shortcut, charsequence, Dimm, Dynamic, dynamic shortcut, enabled, ForegroundColorSpan, getSystemService, intent array, Label, launcher, long press, LongLabel, MainActivity, manifest, meta-data, nougat, now launcher, PIN, Pixel Launcher, rank, Resource, Runtime, setDynamicShortcuts, setIcon, setIntent, setIntents, setLongLabel, setRank, setShortLabel, setSpan, shortCut, shortcut max count, shortcut xml, shortcut 순서, shortcutDisabledMessage, ShortcutInfo, ShortcutInfo.Builder, shortcutLongLabel, ShortcutManager, shortcuts, shortcutShortLabel, spannable, spannablestringbuilder, static, static shortcut, stylish, tutorial, updateShortcuts, xml, [android] App Shortcuts Tutorial, 고정, 바로가기, 바로가기 옵션, 순서, 하단


-

Android N ( Nougat ) 7.1 버전부터 사용 가능.



-

Shortcut 이 설정된 앱은 롱 프레스를 하면 위의 스샷처럼 바로가기 옵션이 나온다.



-

간단한 정리

     특정 동작을 진입과 동시에 할 수 있다는 것이 키 포인트

     static 과 dynamic shortcut 2가지 종류가 있다.

     static 은 xml 에 정의하여 앱 안에 내용이 정의되어 있다. ( 수정하려면 앱 수정 및 재배포를 해야 한다. )

     dynamic 은 Runtime 에 바꿀 수 있다.

     shortcut 으로 앱을 열 때 activity back stack 을 만들 수 있다.

     static shortcut 은 항상 하단에 오며, xml 수정을 통해 순서를 바꿀 수 있다.

     dynamic shortcut 은 static shortcut 위쪽에 오며, rank 를 이용하여 dynamic shortcut 간 순서를 조정할 수 있다.

     shortcut 의 label 은 CharSequence 이기 때문에 Spannable 을 먹일 수 있고, 이것이 적용된다.

     shortcut 은 총 5개까지 정의할 수 있다.



-

Shortcut 적용은 Android Nougat 7.1 단말과 shortcut 을 지원하는 Launcher 가 있어야 한다.

글을 쓰는 시점에 잘 알려진 Launcher 는 Pixel Launcher 또는 Now Launcher 이다.




Static Shortcut


1. Shortcut XML 작성

<?xml version="1.0" encoding="utf-8"?>  

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">  

  <shortcut

    android:enabled=“true”

    android:icon="@drawable/ic_static_shortcut"

    android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"

    android:shortcutId="static"

    android:shortcutLongLabel="@string/static_shortcut_long_label"

    android:shortcutShortLabel="@string/static_shortcut_short_label”>

    <intent

      android:action="android.intent.action.VIEW"

      android:targetClass="com.catinean.appshortcutsdemo.StaticShortcutActivity"

      android:targetPackage="com.catinean.appshortcutsdemo" />

  </shortcut>

</shortcuts>


enabled

     false 이면 노출이 안되거나, Shortcut 이 Launcher 에 “고정(Pin)” 되어 있다면, 회색으로 dimm 이 되고 선택하면 shortcutDisabledMessage 가 Toast 로 표시된다.


 shortcutShortLabel

     대부분의 경우 이 녀석이 display 가 된다. Launcher 에 여유공간이 많을 때에만 간혹 LongLabel 이 나올 수 있다.




2. Manifest 의 MainActivity 의 level 에 static shortcut 을 정의한 xml 파일을 meta-data 로 등록해준다.

<activity

  ...

  <intent-filter>

    <action android:name=“android.intent.action.MAIN” />

    <category android:name="android.intent.category.LAUNCHER" />

    <category android:name="android.intent.category.DEFAULT" />

  </intent-filter>

  <meta-data

        android:name="android.app.shortcuts"

        android:resource="@xml/shortcuts" />

</application>




3. 바로 결과를 확인할 수 있다.

7.1, activity backstack, activity stack, Android N, android.app.shortcuts, app shortcut, charsequence, Dimm, Dynamic, dynamic shortcut, enabled, ForegroundColorSpan, getSystemService, intent array, Label, launcher, long press, LongLabel, MainActivity, manifest, meta-data, nougat, now launcher, PIN, Pixel Launcher, rank, Resource, Runtime, setDynamicShortcuts, setIcon, setIntent, setIntents, setLongLabel, setRank, setShortLabel, setSpan, shortCut, shortcut max count, shortcut xml, shortcut 순서, shortcutDisabledMessage, ShortcutInfo, ShortcutInfo.Builder, shortcutLongLabel, ShortcutManager, shortcuts, shortcutShortLabel, spannable, spannablestringbuilder, static, static shortcut, stylish, tutorial, updateShortcuts, xml, [android] App Shortcuts Tutorial, 고정, 바로가기, 바로가기 옵션, 순서, 하단

 



4. Activity Stack 을 쌓고 싶다면, shortcut 정의에 intent 를 순차적으로 적어주면 된다.

<?xml version="1.0" encoding="utf-8"?>  

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">  

  <shortcut

  ...>

    <intent

      android:action="android.intent.action.MAIN"

      android:targetClass="com.catinean.appshortcutsdemo.MainActivity"

      android:targetPackage="com.catinean.appshortcutsdemo" />

    <intent

      android:action="android.intent.action.VIEW"

      android:targetClass="com.catinean.appshortcutsdemo.StaticShortcutActivity"

      android:targetPackage="com.catinean.appshortcutsdemo" />

  </shortcut>

</shortcuts>


 

 



Dynamic Shortcut


-

ShortcutManager 와 ShortcutInfo.Builder 를 통해 생성할 수 있다.


https://developer.android.com/reference/android/content/pm/ShortcutManager.html

https://developer.android.com/reference/android/content/pm/ShortcutInfo.Builder.html

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo webShortcut = new ShortcutInfo.Builder(this, "shortcut_web")

        .setShortLabel("catinean.com")

        .setLongLabel("Open catinean.com web site")

        .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut))

        .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://catinean.com")))

        .build();

shortcutManager.setDynamicShortcuts(Collections.singletonList(webShortcut));



-

Activity Stack 을 쌓고 싶다면, setIntent 대신 setIntents 를 통해 Intent[] array 를 전달해주면 된다.



-

Dynmaic Shortcut 은 setRank(int) 를 통해 순서를 정할 수 있다.

ShortcutInfo webShortcut = new ShortcutInfo.Builder(MainActivity.this, "shortcut_web")

                  .setRank(1)

                  .build();


ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(MainActivity.this, "shortcut_dynamic")

      .setRank(0)

      .build();


shortcutManager.updateShortcuts(Arrays.asList(webShortcut, dynamicShortcut));


rank 는 낮은 값일수록 아래쪽으로 간다. 당연히 높은 같이 더 위쪽으로 간다.


cf) 위의 코드에서 또 한가지 주목할만한 것은 동일한 id 로 이미 shortcut 이 정의되어 있다면 ShortcutManager.updateShortcuts 를 통해 해당 shortcut 을 업데이트 하여 모든 정보를 다 정의해줄 필요는 없다.





기타 내용


-

Dynamic shortcut 의 경우 Label 이 CharSequence 를 받기 때문에 Spannable 을 이용하여 글자를 stylish 하게 가져갈 수 있다.

( 필자가 알기론 framework 기능만 사용하여 xml 을 통해 Spannable 을 만들고 적용하는 것은 없는 것으로 알고 있다. 있다면 static shortcut 도 가능할듯.. )

ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(android.R.color.holo_red_dark, getTheme()));

String label = "catinean.com";

SpannableStringBuilder colouredLabel = new SpannableStringBuilder(label);

colouredLabel.setSpan(colorSpan, 0, label.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);


ShortcutInfo webShortcut = new ShortcutInfo.Builder(MainActivity.this, "shortcut_web")

        .setShortLabel(colouredLabel)

        .setRank(1)

        .build();


7.1, activity backstack, activity stack, Android N, android.app.shortcuts, app shortcut, charsequence, Dimm, Dynamic, dynamic shortcut, enabled, ForegroundColorSpan, getSystemService, intent array, Label, launcher, long press, LongLabel, MainActivity, manifest, meta-data, nougat, now launcher, PIN, Pixel Launcher, rank, Resource, Runtime, setDynamicShortcuts, setIcon, setIntent, setIntents, setLongLabel, setRank, setShortLabel, setSpan, shortCut, shortcut max count, shortcut xml, shortcut 순서, shortcutDisabledMessage, ShortcutInfo, ShortcutInfo.Builder, shortcutLongLabel, ShortcutManager, shortcuts, shortcutShortLabel, spannable, spannablestringbuilder, static, static shortcut, stylish, tutorial, updateShortcuts, xml, [android] App Shortcuts Tutorial, 고정, 바로가기, 바로가기 옵션, 순서, 하단




반응형

댓글