建立裝置群組篩選器 creating-device-group-filters

NOTE
Adobe建議針對需要以單頁應用程式框架為基礎的使用者端轉譯(例如React)的專案,使用SPA編輯器。 了解更多

建立裝置群組篩選器,以定義一組裝置功能需求。 建立您需要的篩選器數,以鎖定所需的裝置功能群組。

設計您的篩選器,以便使用它們的組合來定義功能群組。 通常,不同裝置群組的功能會重疊。 因此,您可能會使用具有多個裝置群組定義的篩選器。

建立篩選器後,您可在以下位置使用它: 群組設定。

篩選器Java™類別 the-filter-java-class

裝置群組篩選器是實作的OSGi元件 com.day.cq.wcm.mobile.api.device.DeviceGroupFilter 介面。 部署時,實作類別會提供篩選服務,可供裝置群組設定使用。

本文所述的解決方案使用Apache Felix Maven SCR外掛程式來促進元件和服務的開發。 因此,範例Java™類別會使用 @Component@Service 註解。 類別的結構如下:

package com.adobe.example.myapp;

import java.util.Map;

import com.day.cq.wcm.mobile.api.device.DeviceGroup;
import com.day.cq.wcm.mobile.api.device.DeviceGroupFilter;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component(metatype = false)
@Service
public class myDeviceGroupFilter implements DeviceGroupFilter {

       public String getDescription() {
  return null;
 }

 public String getTitle() {
  return null;
 }

 public boolean matches(DeviceGroup arg0, String arg1, Map arg2) {
  return false;
 }

}

提供下列方法的程式碼:

  • getDescription:傳回篩選器說明。 說明會顯示在「裝置群組設定」對話方塊中。
  • getTitle:傳回篩選的名稱。 為裝置群組選取篩選器時,名稱會出現。
  • matches:判斷裝置是否具備必要的功能。

提供篩選器名稱和說明 providing-the-filter-name-and-description

getTitlegetDescription 方法會分別傳回篩選器名稱和說明。 下列程式碼說明最簡單的實施:

public String getDescription() {
    return "An example device group filter";
}

public String getTitle() {
 return "myFilter";
}

以硬式編碼撰寫名稱和說明文字,已足以應付多語言撰寫環境。 請考慮將字串外部化以供多語言使用,或啟用變更字串而不重新編譯原始程式碼。

根據篩選條件進行評估 evaluating-against-filter-criteria

matches 函式傳回 true 如果裝置功能滿足所有篩選條件。 評估方法引數中提供的資訊,以判斷裝置是否屬於群組。 下列值會提供為引數:

  • devicegroup物件
  • 使用者代理程式的名稱
  • 包含裝置功能的對映物件。 Map鍵是WURFL™功能名稱,值是WURFL™資料庫的對應值。

com.day.cq.wcm.mobile.api.devicespecs.DeviceSpecsConstants 介麵包含靜態欄位中WURFL™功能名稱的子集。 從裝置功能地圖擷取值時,使用這些欄位常數作為索引鍵。

例如,下列程式碼範例會判斷裝置是否支援CSS:

boolean cssSupport = true;
cssSupport = NumberUtils.toInt(capabilities.get(DeviceSpecsConstants.DSPEC_XHTML_SUPPORT_LEVEL)) > 1;

org.apache.commons.lang.math 套件提供 NumberUtils 類別。

NOTE
請確定部署至AEM的WURFL™資料庫包含您用來作為篩選條件的功能。 (請參閱 裝置偵測.)

熒幕大小的篩選範例 example-filter-for-screen-size

後續的範例DeviceGroupFilter實作會決定裝置的實體大小是否符合最低需求。 此篩選器的目的在於將粒度新增至觸控裝置群組。 無論實體熒幕大小為何,應用程式UI中的按鈕大小都應相同。 其他專案(例如文字)的大小會有所不同。 此篩選器可讓您動態選取特定CSS,以控制UI元素的大小。

此篩選器會將大小條件套用至 physical_screen_heightphysical_screen_width wurfl™屬性名稱。

package com.adobe.example.myapp;

import java.util.Map;

import com.day.cq.wcm.mobile.api.device.DeviceGroup;
import com.day.cq.wcm.mobile.api.device.DeviceGroupFilter;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component(metatype = false)
@Service
@SuppressWarnings("unused")
public class ScreenSizeLarge implements DeviceGroupFilter {
    private int len=400;
    private int wid=200;
    public String getDescription() {

        return "Requires the physical size of the screen to have minimum dimensions " + len + "x" + wid+".";
    }

    public String getTitle() {
        return "Screen Size Large ("+len + "x" + wid+")";
    }

    public boolean matches(DeviceGroup deviceGroup, String userAgent,
            Map<String, String> deviceCapabilities) {

        boolean longEnough=true;
        boolean wideEnough=false;
        int dimension1=NumberUtils.toInt(deviceCapabilities.get("physical_screen_height"));
        int dimension2=NumberUtils.toInt(deviceCapabilities.get("physical_screen_width"));
        if(dimension1>dimension2){
            longEnough=dimension1>=len;
            wideEnough=dimension2>=wid;
        }else{
            longEnough=dimension2>=len;
            wideEnough=dimension1>=wid;
        }

        return longEnough && wideEnough;
    }
}

getTitle方法傳回的String值會出現在裝置群組屬性的下拉式清單中。

filteraddtogroup

getTitle和getDescription方法傳回的String值會包含在裝置群組摘要頁面的底部。

篩選器說明

Maven POM檔案 the-maven-pom-file

如果您使用Maven建置應用程式,下列POM程式碼將相當實用。 POM會參照數個必要的外掛程式和相依性。

外掛程式:

  • Apache Maven編譯器外掛程式:從原始程式碼編譯Java™類別。
  • Apache Felix Maven套件組合外掛程式:建立套件組合和資訊清單
  • Apache Felix Maven SCR外掛程式:建立元件描述元檔案並設定服務元件資訊清單標頭。

相依性:

  • cq-wcm-mobile-api-5.5.2.jar:提供DeviceGroup和DeviceGroupFilter介面。

  • org.apache.felix.scr.annotations.jar:提供元件和服務註解。

DeviceGroup和DeviceGroupFilter介面包含在Day Communique 5 WCM Mobile API套件組合中。 Felix註解包含在Apache Felix Declarative Services套件組合中。 您可以從公用Adobe存放庫取得此JAR檔案。

編寫時,5.5.2是AEM最新版本中的WCM Mobile API套件組合版本。 使用AdobeWeb主控台(https://localhost:4502/system/console/bundles),以確保這是部署在您的環境中的套件組合版本。

POM: (您的POM使用不同的groupId和版本。)

<project xmlns="https://maven.apache.org/POM/4.0.0"
        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.adobe.example.myapp</groupId>
      <artifactId>devicefilter</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>my app device filter</name>
      <url>https://dev.day.com/docs/en/cq/current.html</url>
  <packaging>bundle</packaging>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-scr-plugin</artifactId>
            <executions>
                  <execution>
                    <id>generate-scr-scrdescriptor</id>
                    <goals>
                          <goal>scr</goal>
                    </goals>
                  </execution>
            </executions>
          </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>1.4.3</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Export-Package>com.adobe.example.myapp.*;version=${project.version}</Export-Package>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
     <dependency>
         <groupId>com.day.cq.wcm</groupId>
         <artifactId>cq-wcm-mobile-api</artifactId>
         <version>5.5.2</version>
         <scope>provided</scope>
     </dependency>
     <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.annotations</artifactId>
        <version>1.6.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
</project>

新增設定檔, 取得內容套件Maven外掛程式 一節提供您的maven設定檔案來使用公共Adobe存放庫。

recommendation-more-help
19ffd973-7af2-44d0-84b5-d547b0dffee2