Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaImplementation of Facebook’s Android Device Year Class Library in Android

Implementation of Facebook’s Android Device Year Class Library in Android

As developers, we always aim to create an application that would run smoothly on millions of devices, however, the performance of practically every application is influenced by the device specifications. There are currently over 16,000 handsets on the market with varying specifications, and the application may perform poorly on some devices when compared to others. Several functions require a fast processor to operate smoothly and may lag on a slow processor. To address this problem, Facebook developed the Device Year Class Library, which uses a simple algorithm to match a device’s RAM, CPU cores, and clock speed to the year in which those characteristics were considered high-end. This enables developers to easily write different logics for the application’s behavior based on the phone’s hardware capabilities. For classification, the Year Class library takes three factors into account:

  • Number of CPU Cores in the device
  • The Clock speed of a CPU core on the device
  • Total RAM of the device

Based on the device hardware, we can optimize a variety of operations, including:

  • Determine whether to display animations or not, as animations may lag on low-end devices.
  • Select whether to load low or high-quality content for faster loading.
  • Suggest a lite app version for devices with low specs.

We’ll now look at how to use this library in an application. We’ll build an application that detects whether or not to display animations based on the device’s year class.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add the library dependency 

Navigate to the Gradle Scripts > build.gradle(Module:app), add the library in the dependencies section, and sync the project.

implementation 'com.facebook.device.yearclass:yearclass:2.1.0'

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 4: Working with the animation.xml file

  • Navigate to app > res
  • Right-click on res and select New > Directory
  • Type anim as the directory name
  • Right-click on anim and select New > Animation Resource File
  • Type animation as the File Name and press OK.

Below is the code for the animation.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
     android:fillAfter="true">
  
    <!-- animation to scale out the view -->
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toYScale="2.0"
        android:toXScale="2.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"/>
</set>


Step 5: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. 

Java




import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
  
import com.facebook.device.yearclass.YearClass;
  
public class MainActivity extends AppCompatActivity {
    
    TextView textView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
  
        // getting the device year class
        int year = YearClass.get(getApplicationContext());
        if (year > 2013) {
            // if device year class is greater than 2013 --> load animation
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation);
            textView.startAnimation(animation);
        } else {
            // load no animation if device year 
              // class is less than 2013
        }
    }
}


Output:  

RELATED ARTICLES

Most Popular

Recent Comments