Changing Channel on Google TV - android

I am trying to write a simple app for Google TV which generates a random number from 1-10 then randomly selects a channel (501-510) and loads it.
I have tried the official google documentation but the official sample project doesnt compile. I have also read Is the GTV Channel Listing/Change API/Sample is broken on LG G2? and tried to adapt this into the google version however the app crashes on loading.
Im sure this must be a simple fix. I do not need to get information about the channel or search for them using the tutorial at https://developers.google.com/tv/android/docs/gtv_provider.
Any help greatly appreciated.

The access to the providers have changed if you use the code below things should improve.
Permissions:
<uses-permission android:name="com.google.android.tv.permission.READ_CHANNELS"/>
<uses-permission android:name="com.google.android.tv.mediadevices.permission.READ_STREAMS"/>
code:
public abstract class ChannelList {
private static ChannelList mCL=null;
public abstract String getPROVIDER_URI();
public abstract String getCALL_SIGN_COLUMN();
public abstract String getURI_COLUMN();
public abstract String getNUMBER_COLUMN();
public abstract String getNAME_COLUMN();
public static ChannelList getChannelList() {
if (mCL != null)
return mCL;
int mGtvLibraryVersion = 0;
try {
Class<?> cl = Class.forName("com.google.android.tv.Version");
mGtvLibraryVersion = cl.getField("GTV_SDK_INT").getInt(null);
} catch (Exception ex) {}
Log.d("Resolution Test", "Version " + mGtvLibraryVersion);
mCL= mGtvLibraryVersion > 0 ? new Version3ChannelList(): new Version2ChannelList();
return mCL;
}
/**
* Use the getChannelList factory to obtain an instance of a subclass of
* ChannelList
*/
private ChannelList() {
}
#Override
public String toString() {
return "SDK Provider: " + getPROVIDER_URI() + "\n" +
"Columns: " + getCALL_SIGN_COLUMN() + " " + getURI_COLUMN() + " " + getNUMBER_COLUMN() + " "
+ getNAME_COLUMN();
}
public static final class Version2ChannelList extends ChannelList {
#Override
public String getPROVIDER_URI() {
return "content://com.google.android.tv.provider/channel_listing";
}
#Override
public String getCALL_SIGN_COLUMN() {
return "callsign";
}
#Override
public String getURI_COLUMN() {
return "channel_uri";
}
#Override
public String getNUMBER_COLUMN() {
return "channel_number";
}
#Override
public String getNAME_COLUMN() {
return "channel_name";
}
}
public static final class Version3ChannelList extends ChannelList {
#Override
public String getPROVIDER_URI() {
return "content://com.google.tv.mediadevicesapp.MediaDevicesProvider/channel_list";
}
#Override
public String getCALL_SIGN_COLUMN() {
return "subName";
}
#Override
public String getURI_COLUMN() {
return "url";
}
#Override
public String getNUMBER_COLUMN() {
return "channelNumber";
}
#Override
public String getNAME_COLUMN() {
return "name";
}
}

Related

How to resolve W/NetworkRequest: errors while trying to download image from Firebase Storage

I'm currently creating an android project (A Vehicle Rental System) with Firebase as a server. Realtime Database is alright, meanwhile, every time I try to access the images stored in the Firebase Storage, it always gives out an error:
W/NetworkRequest: no auth token for request
W/NetworkRequest: No App Check token for request.
W/NetworkRequest: error sending network request GET /*insert imageURI here*/ failed
W/ExponenentialBackoff: network unavailable, sleeping.
Here is my CustomAdapter:
private ArrayList<ModelItem> modelItemsArrayList;
private OnItemClickListener onItemClickListener;
private static final String TAG = "ModelAdapter: ";
private FirebaseStorage storage;
private StorageReference storageReference;
private Context context;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
onItemClickListener = listener;
}
public static class ModelAdapterViewHolder extends RecyclerView.ViewHolder{
public ImageView modelImageView;
public TextView modelTextView;
public TextView brandTextView;
public CardView modelCardView;
public ModelAdapterViewHolder(#NonNull View itemView, OnItemClickListener listener) {
super(itemView);
modelImageView = itemView.findViewById(R.id.cvFrontProfileImageView);
modelTextView = itemView.findViewById(R.id.cvModelTextView);
brandTextView = itemView.findViewById(R.id.cvBrandTextView);
itemView.setOnClickListener(view -> {
if(listener != null) {
int position = getBindingAdapterPosition();
if(position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
});
}
}
public ModelAdapter(ArrayList<ModelItem> modelItemsArrayList) {
Log.d(TAG, "ModelAdapter: called");
this.modelItemsArrayList = modelItemsArrayList;
Log.d(TAG, "ModelAdapter: modelItemsArrayList.size() = " + modelItemsArrayList.size());
}
#NonNull
#Override
public ModelAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_item_cardview, parent, false);
return new ModelAdapterViewHolder(view, onItemClickListener);
}
#Override
public void onBindViewHolder(#NonNull ModelAdapter.ModelAdapterViewHolder holder, int position) {
//enter data from Firebase here
ModelItem currentItem = modelItemsArrayList.get(position);
Log.d(TAG, "modelItemsArraylist.size() = " + modelItemsArrayList.size());
storage = FirebaseStorage.getInstance();
storageReference = storage.getReferenceFromUrl("gs://roadtrip-7a905.appspot.com/")
.child(currentItem.getmImageResource());
try {
Log.d(TAG, "onBindViewHolder: inside try catch nest");
final File file = File.createTempFile("image", "jpg");
storageReference.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
holder.modelImageView.setImageBitmap(bitmap);
Log.d(TAG, "onBindViewHolder: " + file.getPath() + " successfully loaded");
}
}).addOnFailureListener(e -> {
//Toast.makeText(context, "Failed to Load image: " + currentItem.getmImageResource(),
//Toast.LENGTH_SHORT).show();
Log.d(TAG, "onBindViewHolder: addOnFailureListener: " + e.toString());
});
} catch (Exception e) {
Log.d(TAG, "onBindViewHolder: " + e.toString());
}
//holder.modelImageView.setImageResource(currentItem.getmImageResource()); ==> setFile from storage here
holder.modelTextView.setText(currentItem.getmModelText());
holder.brandTextView.setText(currentItem.getmBrandText());
Log.d(TAG, currentItem.getmImageResource() + "\n"
+ currentItem.getmModelText() + "\n"
+ currentItem.getmBrandText());
}
#Override
public int getItemCount() {
return modelItemsArrayList.size();
}
And here is my CustomClass
package com.example.roadtrip;
public class ModelItem {
private String mImageResource;
private String mModelText;
private String mBrandText;
public ModelItem() {
}
public ModelItem(String mImageResource, String mModelText, String mBrandText) {
this.mImageResource = mImageResource;
this.mModelText = mModelText;
this.mBrandText = mBrandText;
}
public String getmImageResource() {
return mImageResource;
}
public void setmImageResource(String mImageResource) {
this.mImageResource = mImageResource;
}
public String getmModelText() {
return mModelText;
}
public void setmModelText(String mModelText) {
this.mModelText = mModelText;
}
public String getmBrandText() {
return mBrandText;
}
public void setmBrandText(String mBrandText) {
this.mBrandText = mBrandText;
}
}
I've been stuck for more than a week already. Can someone help me?
P.S. It's my first time posting here. I apologize for the Logs in the code, I was just trying to debug.

How can I use setText instead of using append in android studio?

I'm trying to use RETROFIT to get an inform about COVID-19 data. And I want to show the latest data, so I tried
content1 = covid_post_data.getPositive() + "\n";
content2 = covid_post_data.getDeath() + "\n";
content3 = " (+" + covid_post_data.getPositiveIncrease() + ")\n";
content4 = " (+" + covid_post_data.getDeathIncrease() + ")\n";
content5 = " Updated : " + covid_post_data.getDate() + "\n";
textViewResult.setText(content1);
textViewResult2.setText(content2);
textViewResult3.setText(content3);
textViewResult4.setText(content4);
textViewResult5.setText(content5);
But it is not working.it didn't show any data. how can i show the latest data in JSON file with using retrofit? below is my whole code of main activity
1.MainActivity
public class COVIDActivity extends AppCompatActivity {
private TextView textViewResult;
private TextView textViewResult2;
private TextView textViewResult3;
private TextView textViewResult4;
private TextView textViewResult5;
private static final String TAG = "COVIDActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.covid_menu);
//How go_back_button works
ImageButton bo_back_Button = (ImageButton) findViewById(R.id.covid_to_main);
bo_back_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent move_main_covid = new Intent(COVIDActivity.this,MainActivity.class);
startActivity(move_main_covid);
}
});
textViewResult = findViewById(R.id.text_view_result_positive);
textViewResult2 = findViewById(R.id.text_view_result_death);
textViewResult3 = findViewById(R.id.text_view_result_positive_increase);
textViewResult4 = findViewById(R.id.text_view_result_death_increase);
textViewResult5 = findViewById(R.id.today_date);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://covidtracking.com/api/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Call<List<COVID_Post_Data>> call = jsonPlaceHolderApi.get_covid_post();
call.enqueue(new Callback<List<COVID_Post_Data>>() {
#Override
public void onResponse(Call<List<COVID_Post_Data>> call, Response<List<COVID_Post_Data>> response) {
if (!response.isSuccessful()) {
textViewResult.setText(("Code: " + response.code()));
return;
}
List<COVID_Post_Data> posts = response.body();
//Give message if fail, TAG is COVIDActivity so that it will show log in this activity
if(posts == null) {
Log.w(TAG,"Did not receive any valid response body");
return;
}
for (COVID_Post_Data covid_post_data : posts) {
String content1,content2,content3,content4,content5 = "";
content1 = covid_post_data.getPositive() + "\n";
content2 = covid_post_data.getDeath() + "\n";
content3 = " (+" + covid_post_data.getPositiveIncrease() + ")\n";
content4 = " (+" + covid_post_data.getDeathIncrease() + ")\n";
content5 = " Updated : " + covid_post_data.getDate() + "\n";
textViewResult.append(content1);
textViewResult2.append(content2);
textViewResult3.append(content3);
textViewResult4.append(content4);
textViewResult5.append(content5);
}
}
2.get JSON file
public interface JsonPlaceHolderApi {
//get data from json about US infection
#GET("us/daily.json")
Call<List<COVID_Post_Data>> get_covid_post();
//get data from json about states infection
#GET("states/daily.json")
Call<List<COVID_Post_Data>> get_covid_post_state();
}
3.getter function
public class COVID_Post_Data {
private String dataChecked;
private int positiveIncrease;
private int negativeIncrease;
private int deathIncrease;
private String state;
private int positive;
private int death;
private int date;
//if the variable is matched with name in json file no need to put #SerializedName here
public String getDataChecked() {
return dataChecked;
}
public int getPositiveIncrease() {
return positiveIncrease;
}
public int getNegativeIncrease() {
return negativeIncrease;
}
public int getDeathIncrease() {
return deathIncrease;
}
public String getState() {
return state;
}
public int getPositive() {
return positive;
}
public int getDeath() { return death; }
public int getDate() {
return date;
}
}

java.lang.String cannot be cast to model.AtvLanguage

I'm using the library Hawk to persist data. I'm facing a problem with the data retrieved.
When I do the next code to check the value of an object stored, but sais it can't cast the value.
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.test.app.model.AtvLanguage
The code:
if(Hawk.contains("ATV_LANGUAGE")){
atvLanguage = (AtvLanguage)Hawk.get("ATV_LANGUAGE"); // Fail point
}else{
atvLanguage.setLanguageCodes("", getString(R.string.str_en));
}
The put method:
public void updateSelectedLanguage(String iso6391, String iso6392){
AtvLanguage atvLanguageTmp = new AtvLanguage(iso6391, iso6392);
Hawk.put("ATV_LANGUAGE", atvLanguageTmp);
}
AtvLanguage pojo:
public class AtvLanguage implements Serializable {
#SerializedName("iso6391")
#Expose
private String iso6391;
#SerializedName("iso6392")
#Expose
private String iso6392;
public AtvLanguage() {
}
public AtvLanguage(String iso6391, String iso6392) {
setLanguageCodes(iso6391, iso6392);
}
public String getIso6391() {
return iso6391;
}
public void setIso6391(String iso6391) {
this.iso6391 = iso6391;
}
public String getIso6392() {
return iso6392;
}
public void setIso6392(String iso6392) {
this.iso6392 = iso6392;
}
// ISO 639-2 is the language code with 3 chars, like ENG or SPA.
public void setLanguageCodes(String iso6391, String iso6392){
if(iso6391.isEmpty() && !iso6392.isEmpty()){
if(iso6392.equalsIgnoreCase("ENG"))
setIso6391("en");
else if(iso6392.equalsIgnoreCase("ESP"))
setIso6391("es");
setIso6392(iso6392);
}else if(!iso6391.isEmpty() && iso6392.isEmpty()){
if(iso6391.equalsIgnoreCase("en"))
setIso6392("ENG");
else if(iso6391.equalsIgnoreCase("es"))
setIso6392("ESP");
setIso6391(iso6391);
}
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AtvLanguage that = (AtvLanguage) o;
return new EqualsBuilder()
.append(getIso6391(), that.getIso6391())
.append(getIso6392(), that.getIso6392())
.isEquals();
}
#Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(getIso6391())
.append(getIso6392())
.toHashCode();
}
#Override
public String toString() {
return "AtvLanguage{" +
"iso6391='" + iso6391 + '\'' +
", iso6392='" + iso6392 + '\'' +
'}';
}
}

Display the ArrayList values into TextVitew

Am working on one android project. i stored the getting response in the ArrayList while parsing from the JsonArray. now i want just display the values to required text views.
Here am doing.
public void updateRedemptionRequestDetails(){
//Dismiss dialog
dlgProgress.dismiss();
// Get the status
String status=redemptionRequestDetailsResponse.getStatus();
if(status.equals("success")){
List<RedemptionRequestDetailsResource> redemptionRequestDetailsResource = redemptionRequestDetailsResponse.getData();
if(!redemptionRequestDetailsResource.isEmpty() && redemptionRequestDetailsResource.size()==0 ){
redemptionRequestDetailsResources.addAll(redemptionRequestDetailsResource);
populateRedemptionDetails(redemptionRequestDetailsResources);
}
}else if ( status.equals("failed")) {
//Show toast based on error code
generalMethods.showToastMessage(context,redemptionRequestDetailsResponse.getErrorcode());
}
}
Can anyone please shed some lights on this.how can i get the values in specified string and display them.
Here my model class
public class RedemptionRequestDetailsResource {
private String rdmId;
private String rdmUniqueBatchTrackingId;
private String rdmLoyaltyId;
private String rdmStatus;
private String rdmCashPaymentStatus;
private String rdmProductCode;
public String getRdmId() {
return rdmId;
}
public void setRdmId(String rdmId) {
this.rdmId = rdmId;
}
public String getRdmUniqueBatchTrackingId() {
return rdmUniqueBatchTrackingId;
}
public void setRdmUniqueBatchTrackingId(String rdmUniqueBatchTrackingId) {
this.rdmUniqueBatchTrackingId = rdmUniqueBatchTrackingId;
}
public String getRdmLoyaltyId() {
return rdmLoyaltyId;
}
public void setRdmLoyaltyId(String rdmLoyaltyId) {
this.rdmLoyaltyId = rdmLoyaltyId;
}
public String getRdmStatus() {
return rdmStatus;
}
public void setRdmStatus(String rdmStatus) {
this.rdmStatus = rdmStatus;
}
public String getRdmCashPaymentStatus() {
return rdmCashPaymentStatus;
}
public void setRdmCashPaymentStatus(String rdmCashPaymentStatus) {
this.rdmCashPaymentStatus = rdmCashPaymentStatus;
}
public String getRdmProductCode() {
return rdmProductCode;
}
public void setRdmProductCode(String rdmProductCode) {
this.rdmProductCode = rdmProductCode;
}
}
here my populateRedemptionDetails method
public void populateRedemptionDetails(List<RedemptionRequestDetailsResource> requestDetailsResource) {
List<RedemptionRequestDetailsResource> redemptionRequestDetailsResource = requestDetailsResource;
TextView txtLoyaltyId = (TextView)rootView.findViewById(R.id.txtLoyaltyId);
txtLoyaltyId.setText(redemptionRequestDetailsResource.getRdmLoylatyId());
}
i tried like this but it throwing error.
The if condition is wrong, it will never enter:
if (!redemptionRequestDetailsResource.isEmpty() && redemptionRequestDetailsResource.size()==0) {
}
it should be:
if (!redemptionRequestDetailsResource.isEmpty()) {}
also, small tip, when using equals, you should put the constant on the left side of the call, like this:
"success".equals(status)
this is to prevent NullPointerExceptions

Getting Nested JsonObjects & Arrays Using Retrofit Library

I got tired using this library, this is my first time using it and made a lot of success ways, but i'm a bit confused in getting the following Json :
{
"Guides":
{
"English": {"ArabicSony":"Test1","ArabicNexus":"Test2","ArabicSamsung":"Test3","ArabicHTC":"Test4"}
,"Arabic": {"EnglishSony":"Test1","EnglishNexus":"Test2","EnglishSamsung":"Test3","EnglishHTC":"Test4"}
}
}
Googled and saw a lot of guides and answered, and made my List like this :
public class PostItem {
List<PostItemArabic> Arabic;
List<PostItemEnglish> English;
}
class PostItemArabic{
private String ArabicSony;
private String ArabicNexus;
private String ArabicSamsung;
private String ArabicHTC;
public String getArabicSony() {
return ArabicSony;
}
public void setArabicSony(String arabicSony) {
ArabicSony = arabicSony;
}
public String getArabicNexus() {
return ArabicNexus;
}
public void setArabicNexus(String arabicNexus) {
ArabicNexus = arabicNexus;
}
public String getArabicSamsung() {
return ArabicSamsung;
}
public void setArabicSamsung(String arabicSamsung) {
ArabicSamsung = arabicSamsung;
}
public String getArabicHTC() {
return ArabicHTC;
}
public void setArabicHTC(String arabicHTC) {
ArabicHTC = arabicHTC;
}
}
class PostItemEnglish{
private String EnglishSony;
private String EnglishNexus;
private String EnglishSamsung;
private String EnglishHTC;
public String getEnglishSony() {
return EnglishSony;
}
public void setEnglishSony(String englishSony) {
EnglishSony = englishSony;
}
public String getEnglishNexus() {
return EnglishNexus;
}
public void setEnglishNexus(String englishNexus) {
EnglishNexus = englishNexus;
}
public String getEnglishSamsung() {
return EnglishSamsung;
}
public void setEnglishSamsung(String englishSamsung) {
EnglishSamsung = englishSamsung;
}
public String getEnglishHTC() {
return EnglishHTC;
}
public void setEnglishHTC(String englishHTC) {
EnglishHTC = englishHTC;
}
}
My Model :
private class Model {
private List<PostItem> Guides;
public List<PostItem> getGuides() {
return Guides;
}
public void setGuides(List<PostItem> roms_center) {
this.Guides = roms_center;
}
}
And printing the result like this :
List<PostItem> Guides = response.body().getGuides();
for(int i = 0 ; i < Guides.size() ; i ++ ) {
for (int b = 0; b < Guides.get(i).English.size() ; b++){
Log.LogInfo("English Result Is: " + Guides.get(i).English.get(i).getEnglishHTC());
Log.LogInfo("English Result Is: " + Guides.get(i).English.get(i).getEnglishNexus());
Log.LogInfo("English Result Is: " + Guides.get(i).English.get(i).getEnglishSamsung());
Log.LogInfo("English Result Is: " + Guides.get(i).English.get(i).getEnglishSony());
}
for (int b = 0; b < Guides.get(i).Arabic.size() ; b++){
Log.LogInfo("Arabic Result Is: " + Guides.get(i).Arabic.get(i).getArabicHTC());
Log.LogInfo("Arabic Result Is: " + Guides.get(i).Arabic.get(i).getArabicNexus());
Log.LogInfo("Arabic Result Is: " + Guides.get(i).Arabic.get(i).getArabicSamsung());
Log.LogInfo("Arabic Result Is: " + Guides.get(i).Arabic.get(i).getArabicSony());
}
}
My work isn't correct, and getting a lot of errors,
Here's the last error i got :
`Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 3 column 18 path $.Guides
What's the way to make it correct ? `
Based on your models when you try to get the guides list your telling retrofit to populate an array. Retrofit is then getting the data and finding that it is a single object and not array. So you need to update your model to reflect the data returned. For example:
class PostItem {
List<Language> mLanguages;
}
class Language{
String mLanguageTitle; //for example english
List<String> mData; //for this is your list of data
}
Then in your activity instead of getting guides you would get just a post item for example:
response.body().getPostItem();
Hope it helps !
First of all, you can use the retrofit Gson library.
You can handle this in two ways:
Option 1: reformat your languages in your json to be an array like Doug says.
{
"Guides":
[
{"Lang":"English","ArabicSony":"Test1","ArabicNexus":"Test2","ArabicSamsung":"Test3","ArabicHTC":"Test4"}
, {"Lang":"Arabic","EnglishSony":"Test1","EnglishNexus":"Test2","EnglishSamsung":"Test3","EnglishHTC":"Test4"}
]
}
Then you will need to redesign your class to reflect this structure.
Like Doug sayd:
class PostItem {
List<Language> mLanguages;
}
Option 2: Create a custom json desirializer in your class. this will take the Json and break it down into whatever structure you want it to be.
public class PostItem implements JsonDeserializer
#Override
public MyDesirializer deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jarabic = (JsonObject) json.get("Arabic");
//whatever manipulations you want to do (fill with your own code)
PostItem item = new PostItem();
item.arabic = jarabic;
...
...
return item;
}

Categories

Resources