News Android#
Introduction#
The News Module in the InFlight Android SDK enables airlines to deliver rich news content, including articles, images, and provider metadata, to passengers via in-flight entertainment systems. This guide provides a step-by-step process for integrating the News Module using the Android SDK’s APIs, designed for airline developers new to the SDK. It covers initialization, fetching providers, categories, article summaries, full articles, and images, with example code for each step..
Step 1: News Service Initialization#
The News Module begins by initializing the news service, which serves as the entry point for all news-related API operations. This service allows access to providers, categories, article summaries, and full articles.
a. Property Declaration
Declare a variable to hold the news service object, which will act as the interface for all news API calls.
b. News Service Initialization
Initialize the news service using the SDK’s initialization API. Once initialized, the service is ready to fetch provider data, categories, and other content.
API Definition
Parameters
-
context: The application context for service initialization.
-
callback: A callback interface (IInFlightCallback) that handles initialization results.
-
inFlight: An instance of the InFlight system for service connectivity.
Callback Interface
-
onInitServiceComplete(Object serviceObject, String serviceName): Returns the initialized news service object on success.
-
onInitServiceFailed(String serviceName, Error error): Returns an error if initialization fails (e.g., network issues).
import aero.panasonic.inflight.services.IInFlightCallback;
import aero.panasonic.inflight.services.InFlight;
NewsV1 liveTextNews ; // Initialize variable
InFlight inFlight = new InFlight(); // InFlight system instance
NewsV1.initService(context, new IInFlightCallback() {
@Override
public void onInitServiceComplete(Object serviceObject, String serviceName) {
liveTextNews = serviceObject; // Store initialized service
}
@Override
public void onInitServiceFailed(String serviceName, InFlight.Error error) {
// Handle initialization error (e.g., show error message)
Log.e("NewsInit", "Initialization failed: " + error.name());
}
}, inFlight);
Step 2: Get the List of News Providers#
After initializing the news service, fetch the list of available news providers from the InFlight system. Providers are third-party content sources (e.g., news agencies) delivering categorized news in multiple languages. This step is required to retrieve categories and articles.
API Definition
Parameters
- listener: ProviderRequestListener – A callback interface, implementing:
- onProviderReceived(List providers): Receives a list of provider objects with id, name, lang, and categoriesUrl.
- onLiveTextNewsError(Error error): Receives errors like LIVE_TEXT_NEWS_ERROR_CONNECTION_ERROR.
Callback Interface
-
onProviderReceived(List providers): Each provider includes:
- id: Unique identifier (String).
- name: Display name (String).
- lang: Language code (e.g., "en") (String).
- categoriesUrl: URL for fetching categories (String).
-
onLiveTextNewsError(Error error): Errors include:
- LIVE_TEXT_NEWS_ERROR_BAD_REQUEST: Invalid parameters.
- LIVE_TEXT_NEWS_ERROR_BAD_RESPONSE: Unexpected server response.
- LIVE_TEXT_NEWS_ERROR_CONNECTION_ERROR: Network failure.
- LIVE_TEXT_NEWS_ERROR_INTERNAL_ERROR: Internal error.
- LIVE_TEXT_NEWS_ERROR_SERVICE_NOT_FOUND: Service unavailable.
- LIVE_TEXT_NEWS_ERROR_UNKNOWN: Unknown error.
liveTextNews.getProvider(new ProviderRequestListener() {
@Override
public void onProviderReceived(List<ProviderV1> providers) {
if (providers.isEmpty()) {
// Handle empty provider list
Log.e("NewsProviders", "No providers available");
return;
}
// Store providerId for category request
}
@Override
public void onLiveTextNewsError(Error error) {
// Handle error (e.g., show error message)
Log.e("NewsProviders", "Failed to fetch providers: " + error.name());
}
});
Step 3: Get the News Categories#
Using a provider’s ID, fetch the associated news categories (e.g., World, Sports). Each category contains a list of article summaries for previewing content.
API Definition
Parameters
-
providerId: String – The unique ID of the provider, obtained from Step 2.
-
listener: CategoriesRequestListener – A callback interface, implementing:
- onCategoriesReceived(List categories): Receives categories with metadata and summaries.
- onLiveTextNewsError(Error error): Receives errors.
Callback Interface
-
onCategoriesReceived(List categories): Each category includes:
- id: Category ID (String).
- name: Display name (String).
- feedType: "CUSTOM" or "RSS" (String).
- parentId: Parent category ID, if applicable (String).
- subCategoryIds: List of child category IDs (List).
- articleAbstracts: Summaries with id, headline, synopsis, imageUrl, timestamp, url, sourceUrl.
-
onLiveTextNewsError(Error error): Same errors as Step 2.
liveTextNews.getCategoryByProviderId(providerId, new CategoriesRequestListener() {
@Override
public void onCategoriesReceived(List<CategoryV1> categories) {
if (categories.isEmpty()) {
// Handle empty category list
Log.e("NewsCategories", "No categories available");
return;
}
List<ArticleAbstractV1> articleSummaries = new ArrayList();
for (Object category : categories) {
articleSummaries.addAll(category.getArticleAbstracts());
Log.d("NewsCategories", "Category: " + category.getName() + " (" + category.getCategoryId() + ")");
}
// Process article summaries
for (ArticleAbstractV1 article : articleSummaries) {
Log.d("NewsArticles", "Article ID: " + article.getId());
}
List<ArticleAbstractV1> = new ArrayList();
for (Object category : categories) {
summaries.addAll(category.getArticleAbstracts());
Log.d("Categories", "Name: " + category.getName() +
", ID: " + category.getId() +
", Feed: " + category.getFeedType() +
", Parent: " + category.getParentId() +
", Subcategories: " + category.getSubCategoryIds());
}
for (ArticleAbstractV1 summary : summaries) {
Log.d("Summaries", "Article: " + summary.getHeadline() +
", ID: " + summary.getId() +
", Synopsis: " + summary.getSynopsis() +
", Image: " + summary.getImageUrl() +
", Source: " + summary.getSourceUrl() +
", Time: " + summary.getTimestamp());
}
}
@Override
public void onLiveTextNewsError(Error error) {
// Handle error
Log.e("NewsCategories", "Failed to fetch categories: " + error.name());
}
});
Step 4: Get the Article in Detail#
Fetch a full article using its ID, including body text, images, and metadata. Two approaches are provided: SDK API (using getArticleById) and HTTP Request (using OkHttp client).
Approach 1: Using SDK API
Use the SDK’s API to fetch the article directly by ID.
API Definition
Parameters
-
articleId: String – Unique article ID from a summary in Step 3.
-
listener: ArticleRequestListener – Callback interface implementing:
- onArticleReceived(Object article): Receives the article object.
- onLiveTextNewsError(Error error): Handles errors.
Callback Interface
- onArticleReceived(Object article): Article includes:
- id: Article ID (String).
- headline: Title (String).
- synopsis: Summary (String).
- body: Full text (String).
- byline: Author name (String).
- timestamp: Publication date (String).
- images: List of images with URLs and captions
liveTextNews.getArticleById(articleId, new NewsV1.ArticleRequestListener() {
@Override
public void onArticleReceived(ArticleV1 article) {
try {
String id = article.getId();
String headline = article.getHeadline();
String synopsis = article.getSynopsis();
String body = article.getBody();
String byline = article.getByline();
String timestamp = article.getTimestamp();
List<ArticleV1.Image> images = article.getImages();
List imageUrls = new ArrayList();
for (ArticleV1.Image : images) {
imageUrls.add(image.getUrl());
}
Log.d("ArticleSDK", "ID: " + id + ", Headline: " + headline +
", Synopsis: " + synopsis + ", Body: " + body +
", Byline: " + byline + ", Time: " + timestamp +
", Images: " + imageUrls);
} catch (Exception e) {
Log.e("ArticleSDK", "Error processing article: " + e.getMessage());
}
}
@Override
public void onLiveTextNewsError(NewsV1.Error error) {
String message = switch (error.name()) {
case "LIVE_TEXT_NEWS_ERROR_BAD_REQUEST" -> "Invalid parameters";
case "LIVE_TEXT_NEWS_ERROR_BAD_RESPONSE" -> "Unexpected response";
case "LIVE_TEXT_NEWS_ERROR_CONNECTION_ERROR" -> "Network error";
case "LIVE_TEXT_NEWS_ERROR_INTERNAL_ERROR" -> "Internal error";
case "LIVE_TEXT_NEWS_ERROR_SERVICE_NOT_FOUND" -> "Service unavailable";
default -> "Unknown error";
};
Log.e("ArticleSDK", "Error: " + message);
}
});
Approach 2: Using HTTP Request
API Definition
- Use OkHttpClient to send an HTTP request to the article’s URL (obtained from the article summary).
Parameters
-
url: The article’s URL from the article summary’s getUrl method.
-
OkHttpClient: An HTTP client for making the request.
Response
-
A JSON object containing:
- body: The full article text.
-
Errors are handled via exceptions (e.g., network failures).
When to Use Article Summary vs. Full Article
-
Article Summary: Use for listing previews (ID, headline, synopsis, image URL).
-
Full Article: Use for displaying detailed content (body text, images).
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.json.JSONObject;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(articleUrl).build(); // articleUrl from summary
try {
Response response = client.newCall(request).execute();
String responseText = response.body().string();
JSONObject jsonObject = new JSONObject(responseText);
String articleBody = jsonObject.getString("body");
Log.d("NewsArticle", "Headline: " + articleHeadline); // From summary
Log.d("NewsArticle", "Body: " + articleBody);
} catch (Exception e) {
Log.e("NewsArticle", "Failed to fetch article: " + e.getMessage());
}
Step 5: Get an Image for the News Article#
Using an image URL from the article summary or full article, fetch the image as a bitmap for display.
API Definition
Parameters
-
url: Image URL.
-
listener: Interface for image results or errors.
Callback Interface
-
onLiveTextNewsImageReceived(Bitmap bitmap, String url): Returns the bitmap.
-
onLiveTextNewsError(Error error): Returns errors (e.g., LIVE_TEXT_NEWS_ERROR_CONNECTION_ERROR).
Error Enum
Same as Step 2.
import android.graphics.Bitmap;
liveTextNews.getLiveTextNewsImage(imageUrl, new LiveTextNewsImageRequestListener() {
@Override
public void onLiveTextNewsImageReceived(Bitmap bitmap, String url) {
Log.d("Image", "Image fetched for URL: " + url);
// Use bitmap (e.g., set to ImageView)
}
@Override
public void onLiveTextNewsError(Error error) {
String message = switch (error.name()) {
case "LIVE_TEXT_NEWS_ERROR_BAD_REQUEST" -> "Invalid parameters";
case "LIVE_TEXT_NEWS_ERROR_BAD_RESPONSE" -> "Unexpected response";
case "LIVE_TEXT_NEWS_ERROR_CONNECTION_ERROR" -> "Network error";
case "LIVE_TEXT_NEWS_ERROR_INTERNAL_ERROR" -> "Internal error";
case "LIVE_TEXT_NEWS_ERROR_SERVICE_NOT_FOUND" -> "Service unavailable";
default -> "Unknown error";
};
Log.e("Image", "Error: " + message);
}
});
Step 6: Subscribe to Real-Time Updates#
Subscribe to notifications for news content updates, enabling real-time refreshes.
API Definition
Parameters
- listener: Interface for update notifications.
Callback Interface
-
onLiveTextNewsUpdated(): Triggered when content updates (e.g., new articles).
-
onLiveTextNewsError(Error error): Returns errors.
liveTextNews.setLiveTextNewsUpdateListener(new LiveTextNewsUpdateListener() {
@Override
public void onLiveTextNewsUpdated() {
Log.d("Update", "Content updated, refreshing...");
// Re-fetch providers/categories/articles
}
@Override
public void onLiveTextNewsError(Error error) {
Log.e("Update", "Update error: " + error.name());
}
});
Version 1.0