提供篩選器名稱和說明

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

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

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

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

根據篩選條件進行評估

如果裝置功能滿足所有篩選條件,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類別。

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

熒幕大小的篩選範例

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

此篩選器會將大小條件套用至physical_screen_height和physical_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值會出現在裝置群組屬性的下拉式清單中。

filteradtogroup

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

filterdescription

Maven POM檔案

如果您使用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套件組合版本。 使用Adobe Web Console (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存放庫。

自動翻譯
使用「語言選取器」檢視此頁面的英文版。
翻譯是否有幫助?

Ask AI

Not sure where to start?
Ask me anything about Adobe products.

Type your question or pick a suggestion below.

Use of this AI chatbot is subject to Adobe's Privacy Policy. Don't share sensitive data. AI responses are not your Content, may be inaccurate, and any offers provided are non-binding. Generative AI Terms.