The error tells me to cast this Call > arrayListCall= RetrofitBuilder.getRecipes();
but i cast it but still is wrong.My goal is to create a widget with the arraylist of ingredients for the selected recipe in the main. The error message shows:
Unable to start receiver com.example.mac.francosbakingapp.Widget.BankingAppWidgetProvider: java.lang.ClassCastException: $Proxy0 cannot be cast to retrofit2.Call
package com.example.mac.francosbakingapp.Widget;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import com.example.mac.francosbakingapp.MainActivity;
import com.example.mac.francosbakingapp.Model.Ingredient;
import com.example.mac.francosbakingapp.Model.Recipe;
import com.example.mac.francosbakingapp.R;
import com.example.mac.francosbakingapp.RetrofitBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Response;
public class RecipeIngredientsWidgetService extends RemoteViewsService{
#Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new ListViewFactory(this.getApplicationContext()) );
}
class ListViewFactory implements RemoteViewsService.RemoteViewsFactory{
private ArrayList<Ingredient> mIngredientsList;
private Context context;
public ListViewFactory( Context context) {
this.context = context;
}
#Override
public void onCreate() {
}
#Override
public void onDataSetChanged() {
loadData();
}
private void loadData() {
Call <ArrayList<Recipe>> arrayListCall= RetrofitBuilder.getRecipes();
try{
Response<ArrayList<Recipe>> arrayListResponse=arrayListCall.execute();
if (arrayListResponse!=null){
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
int position=sharedPreferences.getInt(MainActivity.POSITION_KEY,0);
Recipe recipe= arrayListResponse.body().get(position);
mIngredientsList= (ArrayList<Ingredient>) recipe.getIngredients();
}
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onDestroy() {
if(mIngredientsList !=null){
mIngredientsList=null;
}
}
#Override
public int getCount() {
return mIngredientsList==null ? 0: mIngredientsList.size();
}
#Override
public RemoteViews getViewAt(int position) {
Ingredient actIngredient=mIngredientsList.get(position);
RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.ingredient_list_item_widget_layout);
String ingredientName= actIngredient.getIngredient();
double ingredientQuantity=actIngredient.getQuantity();
String ingredientMeasure=actIngredient.getMeasure();
String ingredientString=String.format("${title}",ingredientName,ingredientQuantity,ingredientMeasure);
remoteViews.setTextViewText(R.id.tv_widget_population,ingredientString);
return remoteViews;
}
#Override
public RemoteViews getLoadingView() {
return null;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return true;
}
}
}
Blockquote
The getRecipes
public static RecipesInterface getRecipes(){
interfaceRecipes= new Retrofit.Builder()
.baseUrl("http://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/")
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create()))
.build().create(RecipesInterface.class);
return interfaceRecipes;
}
Shows the error in the code
The problem i have is in the cast of;
Call > arrayListCall= RetrofitBuilder.getRecipes();
Complete project on :
https://github.com/franquicidad/FrancosBakingApp
Related
Found a tutorial online for building a simple blockchain app on android studio but when I finished it has key missing declarations which are coming up as unused. My capabilities allow me to resolve most minor errors through the Run and Logcat monitors while debugging but a series of stacked issues like this seems to pose too large of an issue. Here are the pages that it specifies as missing declarations:
BlockChainManager
package com.example.blockchain.managers;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.blockchain.adapter.BlockAdapter;
import com.example.blockchain.models.BlockModel;
import java.util.ArrayList;
import java.util.List;
public class BlockChainManager {
private final int difficulty;
private final List<BlockModel> blocks;
public final BlockAdapter adapter;
public BlockChainManager(int difficulty, #NonNull Context context) {
this.difficulty = difficulty;
blocks = new ArrayList<>();
BlockModel block = new BlockModel(0,System.currentTimeMillis(),null,"Genesis Block");
block.mineBlock(difficulty);
blocks.add(block);
adapter = new BlockAdapter(blocks,context);
}
public BlockModel newBlock(String data){
BlockModel latestBlock = lastestBlock();
return new BlockModel(latestBlock.getIndex()+1,System.currentTimeMillis(),latestBlock.getHash(),data);
}
private BlockModel lastestBlock() {
return blocks.get(blocks.size()-1);
}
public void addBlock(BlockModel block){
if (block!= null){
block.mineBlock(difficulty);
blocks.add(block);
}
}
private boolean isFirstBlockValid(){
BlockModel firstBlock = blocks.get(0);
if (firstBlock.getIndex()!=0){
return false;
}
if (firstBlock.getPreviousHash()!=null){
return false;
}
return firstBlock.getHash()!=null &&
BlockModel.calculateHash_detail(firstBlock).equals(firstBlock.getHash());
}
private boolean isValidNewBlock(#Nullable BlockModel newBlock, #Nullable BlockModel previousBlock){
if (newBlock!=null && previousBlock!= null){
if (previousBlock.getIndex()+1!=newBlock.getIndex()){
return false;
}
if (newBlock.getPreviousHash()==null || !newBlock.getPreviousHash().equals(newBlock.getData())){
return false;
}
return newBlock.getHash()!=null &&
BlockModel.calculateHash_detail(newBlock).equals(newBlock.getHash());
}
return false;
}
public boolean isBlockChainValid(){
if (!isFirstBlockValid()){
return false;
}
for (int i = 1 ; i<blocks.size();i++){
BlockModel currentBlock = blocks.get(i);
BlockModel previousBlock = blocks.get(i-1);
if (!isValidNewBlock(currentBlock,previousBlock))
return false;
}
return true;
}
}
BlockModel
package com.example.blockchain.models;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class BlockModel {
private int index,nonce;
private long timestamp;
private String hash,previousHash,data;
public BlockModel(int index, long timestamp, String previousHash, String data) {
this.index = index;
this.timestamp = timestamp;
this.previousHash = previousHash;
this.data = data;
nonce = 0;
hash = BlockModel.calculateHash_detail(this);
}
public static String calculateHash_detail(BlockModel blockModel) {
if (blockModel!= null){
MessageDigest messageDigest;
try{
messageDigest = MessageDigest.getInstance("SHA-256");
}
catch (NoSuchAlgorithmException e){
return null;
}
String txt = blockModel.str();
final byte[] bytes = messageDigest.digest(txt.getBytes());
final StringBuilder builder = new StringBuilder();
for (final byte b: bytes){
String hex = Integer.toHexString(0xff & b);
if (hex.length()==1){
builder.append('0');
}
builder.append(hex);
}
return builder.toString();
}
return null;
}
private String str() {
return index + timestamp + previousHash + data + nonce;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getNonce() {
return nonce;
}
public void setNonce(int nonce) {
this.nonce = nonce;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public String getPreviousHash() {
return previousHash;
}
public void setPreviousHash(String previousHash) {
this.previousHash = previousHash;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public void mineBlock(int difficulty){
nonce = 0;
while(!getHash().substring(0,difficulty).equals(addZeros(difficulty))){
nonce++;
hash = BlockModel.calculateHash_detail(this);
}
}
private String addZeros(int difficulty) {
StringBuilder builder = new StringBuilder();
for (int i = 0 ; i<difficulty; i++) {
builder.append('0');
}
return builder.toString();
}
}
CipherUtils
package com.example.blockchain.utils;
import android.util.Base64;
import androidx.annotation.NonNull;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class CipherUtils {
private static final String PASSWORD = "Saf fur yoU";
private static final String ALGORITHM = "DES";
public static String encryptIt(#NonNull String value){
try{
DESKeySpec keySpec = new DESKeySpec(PASSWORD.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey key = keyFactory.generateSecret(keySpec);
byte [] clearText = value.getBytes(StandardCharsets.UTF_8);
Cipher cipher = Cipher.getInstance("ALGORITHM");
cipher.init(Cipher.ENCRYPT_MODE,key);
return Base64.encodeToString(cipher.doFinal(clearText),Base64.DEFAULT);
}
catch (InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException e){
e.printStackTrace();
}
return value;
}
public static String decryptIt(#NonNull String value){
try{
DESKeySpec keySpec = new DESKeySpec(PASSWORD.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey key = keyFactory.generateSecret(keySpec);
byte [] encryptedPwdBytes = Base64.decode(value,Base64.DEFAULT);
Cipher cipher = Cipher.getInstance("ALGORITHM");
cipher.init(Cipher.DECRYPT_MODE,key);
byte [] decryptedValueBytes = (cipher.doFinal(encryptedPwdBytes));
return new String(decryptedValueBytes);
}
catch (InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException e){
e.printStackTrace();
}
return value;
}
}
MainActivity
package com.example.blockchain;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.example.blockchain.fragments.PowFragment;
import com.example.blockchain.databinding.ActivityMainBinding;
import com.example.blockchain.databinding.ContentMainBinding;
import com.example.blockchain.managers.BlockChainManager;
import com.example.blockchain.managers.SharedPreferencesManager;
import com.example.blockchain.utils.CipherUtils;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ContentMainBinding viewBindingContent;
private ProgressDialog progressDialog;
private SharedPreferencesManager prefs;
private BlockChainManager blockChain;
private boolean isEncryptionActivated, isDarkActivated;
private static final String TAG_POW_DIALOG = "proof_of_work_dialog";
#Override
protected void onCreate(Bundle savedInstanceState) {
prefs = new SharedPreferencesManager(this);
isDarkActivated = prefs.isDarkTheme();
PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
boolean isPowerSaveMode = false;
if (powerManager!=null){
isPowerSaveMode = powerManager.isPowerSaveMode();
}
if(isPowerSaveMode){
isPowerSaveMode = powerManager.isPowerSaveMode();
}else{
if (isDarkActivated){
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES
);
}else{
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_NO
);
}
}
super.onCreate(savedInstanceState);
final ActivityMainBinding viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
viewBindingContent = ContentMainBinding.bind(viewBinding.contentMain.getRoot());
setContentView(R.layout.activity_main);
isEncryptionActivated = prefs.getEncryptionStatus();
viewBindingContent.recyclerContent.setHasFixedSize(true);
viewBindingContent.recyclerContent.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL,false));
showProgressDialog(getResources().getString(R.string.text_creating_block_chain));
new Thread(() -> runOnUiThread(() -> {
blockChain = new BlockChainManager(prefs.getPowValue(),this);
viewBindingContent.recyclerContent.setAdapter(blockChain.adapter);
cancelProgressDialog(progressDialog);
})).start();
viewBindingContent.btnSendData.setOnClickListener(this);
}
#Override
protected void onResume() {
super.onResume();
}
private void startBlockChain() {
showProgressDialog(getResources().getString(R.string.text_mining_blocks));
runOnUiThread(() -> {
if (blockChain != null && viewBindingContent.editMessage.getText() != null && viewBindingContent.recyclerContent.getAdapter() != null) {
String message = viewBindingContent.editMessage.getText().toString();
if (!message.isEmpty()) {
if (!isEncryptionActivated) {
blockChain.addBlock(blockChain.newBlock(message));
} else {
try {
blockChain.addBlock(blockChain.newBlock(CipherUtils.encryptIt(message).trim()));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Something fishy happened", Toast.LENGTH_SHORT).show();
}
}
viewBindingContent.recyclerContent.scrollToPosition(blockChain.adapter.getItemCount() - 1);
if (blockChain.isBlockChainValid()) {
viewBindingContent.recyclerContent.getAdapter().notifyDataSetChanged();
viewBindingContent.editMessage.setText("");
} else {
Toast.makeText(this, "BlockChain Corrupted", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Error empty data", Toast.LENGTH_SHORT).show();
}
cancelProgressDialog(progressDialog);
} else {
Toast.makeText(this, "Something wrong happened", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onClick(View view) {
if (view.getId()==R.id.btn_send_data)
startBlockChain();
}
private void showProgressDialog(#NonNull String loadingMessage){
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(loadingMessage);
progressDialog.setCancelable(false);
progressDialog.setMax(100);
progressDialog.show();
}
private void cancelProgressDialog(#NonNull ProgressDialog progressDialog){
if(progressDialog!=null){
progressDialog.cancel();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu1,menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkEncrypt = menu.findItem(R.id.action_encrypt);
checkEncrypt.setChecked(isEncryptionActivated);
MenuItem checkTheme = menu.findItem(R.id.action_dark);
checkTheme.setChecked(isDarkActivated);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_pow:
PowFragment powFragment = PowFragment.newInstance();
powFragment.show(this.getSupportFragmentManager(),TAG_POW_DIALOG);
break;
case R.id.action_encrypt:
isEncryptionActivated = !item.isChecked();
item.setChecked(isEncryptionActivated);
if (item.isChecked()){
Toast.makeText(this, "Message Encryption ON", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Message Encryption OFF", Toast.LENGTH_SHORT).show();
}
prefs.setEncryptionStatus(isEncryptionActivated);
return true;
case R.id.action_dark:
isDarkActivated =!item.isChecked();
item.setChecked(isDarkActivated);
prefs.setDarkTheme(isDarkActivated);
Intent intent = this.getPackageManager().getLaunchIntentForPackage(this.getPackageName());
startActivity(intent);
finish();
return true;
case R.id.action_exit:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
}
PoWFragment
package com.example.blockchain.fragments;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import com.example.blockchain.R;
import com.example.blockchain.databinding.FragmentPowBinding;
import com.example.blockchain.managers.SharedPreferencesManager;
/**
* A simple {#link Fragment} subclass.
* Use the {#link PowFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class PowFragment extends DialogFragment implements View.OnClickListener {
private FragmentPowBinding viewBinding;
private Context mcontext;
private SharedPreferencesManager prefs;
public PowFragment() {
// Required empty public constructor
}
public static PowFragment newInstance() {
return new PowFragment();
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
mcontext = context.getApplicationContext();
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
viewBinding = FragmentPowBinding.inflate(getLayoutInflater(), container, false);
return viewBinding.getRoot();
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
prefs = new SharedPreferencesManager(mcontext);
viewBinding.edtSetPow.setText(String.valueOf(prefs.getPowValue()));
viewBinding.btnClose.setOnClickListener(this);
viewBinding.btnContinue.setOnClickListener(this);
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialog.getWindow() != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
return dialog;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_close:
dismiss();
break;
case R.id.btn_continue:
if (viewBinding.edtSetPow.getText() != null) {
String pow = viewBinding.edtSetPow.getText().toString();
prefs.setPowValue(Integer.parseInt(pow));
if (getActivity() != null) {
Intent intent = mcontext.getPackageManager().getLaunchIntentForPackage(mcontext.getPackageName());
startActivity(intent);
getActivity().finish();
} else {
dismiss();
}
break;
}
}
}
#Override
public void onDetach() {
super.onDetach();
viewBinding = null;
mcontext = null;
}
}
SharedPreferencesManager
package com.example.blockchain.managers;
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPreferencesManager {
private final SharedPreferences sharedPreferences;
private final SharedPreferences.Editor editor;
private static final String PREFERENCES_DATA = "com.example.blockchain";
private static final String ENCRYPTION_STATUS = "encryption_status";
private static final String DARK_THEME = "dark_theme";
private static final String PROOF_OF_WORK = "proof_of_work";
public static final int DEFAULT_PROOF_OF_WORK = 2;
public SharedPreferencesManager(Context context) {
sharedPreferences = context.getSharedPreferences(PREFERENCES_DATA,Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.apply();
}
public void setPowValue(int powValue){
editor.putInt(PROOF_OF_WORK,powValue);
editor.commit();
}
public int getPowValue(){
return sharedPreferences.getInt(PROOF_OF_WORK,DEFAULT_PROOF_OF_WORK);
}
public void setEncryptionStatus(boolean isActivated){
editor.putBoolean(ENCRYPTION_STATUS,isActivated);
editor.commit();
}
public boolean getEncryptionStatus(){
return sharedPreferences.getBoolean(ENCRYPTION_STATUS,false);
}
public void setDarkTheme(boolean isActivated){
editor.putBoolean(DARK_THEME,isActivated);
editor.commit();
}
public boolean isDarkTheme(){
return sharedPreferences.getBoolean(DARK_THEME,false);
}
}
If for any reason the Manifest will help:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blockchain">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:fullBackupContent="#xml/backup_descriptor">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
These errors seem to be centered around a specific class meant to integrate the BlockChain into the App FrameWork, but this has used its declarations:
Block Adapter
package com.example.blockchain.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.example.blockchain.R;
import com.example.blockchain.models.BlockModel;
import com.example.blockchain.viewholder.RecyclerViewHolder;
import java.util.Date;
import java.util.List;
public class BlockAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private final List<BlockModel> blocks;
private final Context mcontext;
private int lastPosition = -1;
public BlockAdapter(#Nullable List<BlockModel> blocks, #NonNull Context mcontext) {
this.blocks = blocks;
this.mcontext = mcontext;
}
#NonNull
#Override
public RecyclerViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType,parent,false);
return new RecyclerViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerViewHolder holder, int position) {
holder.txtIndex.setText(String.format(
mcontext.getString(R.string.title_block_number),blocks.get(position).getIndex()
));
holder.txtPreviousHash.setText(blocks.get(position).getPreviousHash()!=null ? blocks.get(position).getPreviousHash(): "Null");
holder.txtTimestamp.setText(String.valueOf(new Date(blocks.get(position).getTimestamp())));
holder.txtData.setText(blocks.get(position).getData());
holder.txtHash.setText(blocks.get(position).getHash());
setAnimation(holder.itemView,position);
}
private void setAnimation(View itemView, int position) {
if (position>lastPosition){
Animation animation = AnimationUtils.loadAnimation(mcontext,android.R.anim.fade_in);
itemView.startAnimation(animation);
lastPosition = position;
}
}
#Override
public int getItemViewType(int position) {
return R.layout.item_block_data;
}
#Override
public int getItemCount() {
return blocks == null ? 0:blocks.size();
}
}
Let me know if anything else might help resolve these issues.
Here is the link to the video however the advertised "FREE" learning actually requires $30 for the source code once you realize what you have received does not work: https://www.youtube.com/watch?v=c5E_yBMt7Io&t=1s
Since I'm not an idiot I did not send someone money simply because they shared a few chopped up videos and asked for it in an email, but the lengthy 7-part series to deceive people into obligatorily purchasing after the "FREE" learning is illegal misrepresentation as well as unethically conning people interested in the fledgling blockchain technologies.
Any help is greatly appreciated!
I am using glide for displaying album art but somehow I am not able to display it. I want to add listener to glide for locating errors but its not working. It shows error saying :
listener(com.bumptech.glide.request.RequestListener) in com.bumptech.glide.DrawableRequestBuilder cannot be applied to (anonymous com.bumptech.glide.request.RequestListener)
PlayListActivity.java :
package com.example.dell_1.myapp3;
import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
public class PlayListActivity extends Activity {
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_list);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.list);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
setContentView(R.layout.activity_android_building_music_player);
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
asd();
}
public void asd(){
File music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
// Tested with music from Windows 7's c:\Users\Public\Music\Sample Music
String mAudioPath = new File(music, "Maid with the Flaxen Hair.mp3").getAbsolutePath();
ImageView imageView = (ImageView) findViewById(R.id.coverart);
Glide
.with(this)
.load(new AudioCover(mAudioPath))
.placeholder(R.drawable.adele1)
.error(R.drawable.adele1)
.listener(new RequestListener<Uri, GlideDrawable>() {
#Override public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// easy
return false;
// impossible?
}
})
.into(imageView)
;
}
}
AudioCover.java :
package com.example.dell_1.myapp3;
import android.content.Context;
import android.media.MediaMetadataRetriever;
import com.bumptech.glide.*;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class AudioCover {
final String path;
public AudioCover(String path) {
this.path = path;
}
}
class AudioCoverLoader implements StreamModelLoader<AudioCover> {
#Override public DataFetcher<InputStream> getResourceFetcher(AudioCover model, int width, int height) {
return new AudioCoverFetcher(model);
}
static class Factory implements ModelLoaderFactory<AudioCover, InputStream> {
#Override public ModelLoader<AudioCover, InputStream> build(Context context, GenericLoaderFactory factories) {
return new AudioCoverLoader();
}
#Override public void teardown() {
}
}
}
class AudioCoverFetcher implements DataFetcher<InputStream> {
private final AudioCover model;
private FileInputStream stream;
public AudioCoverFetcher(AudioCover model) {
this.model = model;
}
#Override public String getId() {
return model.path;
}
#Override public InputStream loadData(Priority priority) throws Exception {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(model.path);
byte[] picture = retriever.getEmbeddedPicture();
if (picture != null) {
return new ByteArrayInputStream(picture);
} else {
return fallback(model.path);
}
} finally {
retriever.release();
}
}
private static final String[] FALLBACKS = {"cover.jpg", "album.jpg", "folder.jpg"};
private InputStream fallback(String path) throws FileNotFoundException {
File parent = new File(path).getParentFile();
for (String fallback : FALLBACKS) {
// TODO make it smarter by enumerating folder contents and filtering for files
// example algorithm for that: http://askubuntu.com/questions/123612/how-do-i-set-album-artwork
File cover = new File(parent, fallback);
if (cover.exists()) {
return stream = new FileInputStream(cover);
}
}
return null;
}
#Override public void cleanup() {
// already cleaned up in loadData and ByteArrayInputStream will be GC'd
if (stream != null) {
try {
stream.close();
} catch (IOException ignore) {
// can't do much about it
}
}
}
#Override public void cancel() {
// cannot cancel
}
}
RequestListener<T, R> expects T to be the model you provided in load(), in your case T = ? super AudioCover (documentation):
// ...
.listener(new RequestListener<AudioCover, GlideDrawable>() {
#Override
public boolean onException(Exception e, AudioCover model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, AudioCover model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
// ...
Note that since version 4.0.0-RC0 RequestListener<T, R> was deprecated in favor of RequestListener<R>, find more information in the new javadoc.
Adding RequestListner
Glide.with(this)
.load(url)
.listener(new RequestListener < Uri, GlideDrawable > () {
#Override public boolean onException(Exception e, Uri model, Target < GlideDrawable > target, boolean isFirstResource) {
return false;
}
#Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target < GlideDrawable > target, boolean isFromMemoryCache, boolean isFirstResource) {
// easy
return false;
// impossible?
}
})
.into(imageView);
I use Android Studio to make an app. I have a file named 'GlobalVariables.java' and this code in it:
public class GlobalVariables extends Application {
public String CallingActivity;
public String getCallVariable() {
return CallingActivity;
}
public void setCallVariable(String Value) {
CallingActivity = Value;
}
}
In the manifest file I have:
<application android:name=".GlobalVariables" .....
I also have a LanguageActivity.java file which has this code:
package com.testApp;
//import android.content.Intent;
//import android.support.v7.app.ActionBarActivity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class LanguageActivity extends ListActivity {
// public class CountrycodeActivity extends ListActivity {
public static final String TAG = "MyActivity";
// public static String RESULT_CONTRYCODE = "countrycode";
public String[] countrynames;
private TypedArray imgs;
private List<Country> countryList;
Locale myLocale;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
populateCountryList();
ArrayAdapter<Country> adapter = new CountryListArrayAdapter(this, countryList);
setListAdapter(adapter);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ReadWriteFile RWFile = new ReadWriteFile();
if (position == 0) {
ChangeLanguage("el", getBaseContext());
try {
RWFile.LangWrite("en",getBaseContext());
Log.e(TAG, "LANG === en");
} catch (IOException e) {
e.printStackTrace();
}
}else{
ChangeLanguage("en", getBaseContext());
try {
RWFile.LangWrite("en",getBaseContext());
Log.e(TAG, "LANG === en");
} catch (IOException e) {
e.printStackTrace();
}
}
imgs.recycle(); //recycle images
finish();
}
});
}
private void populateCountryList() {
countryList = new ArrayList<Country>();
countrynames = getResources().getStringArray(R.array.country_names);
//countrycodes = getResources().getStringArray(R.array.country_codes);
imgs = getResources().obtainTypedArray(R.array.country_flags);
for(int i = 0; i < countrynames.length; i++){
countryList.add(new Country(countrynames[i], imgs.getDrawable(i)));
}
}
public class Country {
private String name;
// private String code;
private Drawable flag;
public Country(String name, Drawable flag){
this.name = name;
// this.code = code;
this.flag = flag;
}
public String getName() {
return name;
}
public Drawable getFlag() {
return flag;
}
// public String getCode() {
// return code;
// }
}
public void ChangeLanguage(String value, Context context){
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(value.toLowerCase());
res.updateConfiguration(conf, dm);
GlobalVariables mApp = ((GlobalVariables)getApplicationContext());
String Activity = mApp.getCallVariable();
if (Activity.equals("Login")){
final Intent intent = new Intent(LanguageActivity.this, LoginActivity.class);
startActivity(intent);
}else if (Activity.equals("Signup")){
final Intent intent = new Intent(LanguageActivity.this, SignupActivity.class);
startActivity(intent);
}
}
}
When I run the code, the app craches with this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
If I change
GlobalVariables mApp = ((GlobalVariables)getApplicationContext());
String Activity = mApp.getCallVariable();
to
String Activity = ((GlobalVariables) this.getApplication()).getCallVariable();
then I get error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.testApp.GlobalVariables.getCallVariable()' on a null object reference
I did many tries but nothing helped. What will solve my problem anyway?
Why do I do this? I want to know which Activity called ChangeLanguage() to restart it to show the selected language.
Dependency injection will solve your problem. If you use dagger2, solve this kind of problem will be better, because will be easier create singletons. If you want i can post here how you can do this. Comment below this answer if you want me to edit this with dagger2 basics.
Add this to the AndroidManifest.xml:
<application
android:name=".GlobalVariables"/>
Try:
public class GlobalVariables extends Application {
public String CallingActivity;
public staric GlobalVariables instance;
#Override
public void onCreate()
{super.onCreate();
this.instance = this;
}
public String getCallVariable() {
return CallingActivity;
}
public void setCallVariable(String Value) {
CallingActivity = Value;
}
public static GlobalVariables getInstance()
{
return instance;
}
}
String Activity = GlobalVariables.getInstance().getCallVariable();
Try replacing the line
GlobalVariables mApp = ((GlobalVariables)getApplicationContext());
with:
GlobalVariables mApp = new GlobalVariables();
And in line :
String Activity = mApp.getCallVariable();
Replace 'Activity' with 'activity' because Activity is a pre-defined word.
Edit 1: IS this is how you tried:
GlobalVariables mApp = GlobalVariables.getInstance();
mApp.setCallVariable(value);
mApp.getCallVariable();
Remote Service
I'm doing a test for Android remote Service.
In the first app module, I make a service, complete as below:
AppService
package com.hqyj.dev.aidltest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class AppService extends Service {
public AppService() {
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IAppServiceRemoteBinder.Stub() {
#Override
public void basicTypes(
int anInt, long aLong,
boolean aBoolean, float aFloat,
double aDouble, String aString)
throws RemoteException {
}
#Override
public void setData(String data)
throws RemoteException {
setRealData(data);
}
#Override
public void registerCallback(IRemoteServiceCallback cb)
throws RemoteException {
AppService.this.callback = cb;
}
#Override
public void unregisterCallback(IRemoteServiceCallback cb)
throws RemoteException {
AppService.this.callback = null;
}
};
}
private IRemoteServiceCallback callback;
#Override
public void onCreate() {
super.onCreate();
System.out.println("Service started");
}
#Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service stop");
}
public void setRealData(String data) {
this.data = data;
System.out.println("data = " + data);
try {
Thread.sleep(1000);
if (callback != null) {
callback.vlueChanged(data);
}
} catch (InterruptedException | RemoteException e) {
e.printStackTrace();
}
}
private String data = "default date";
}
And their are two AIDL files:
IAppServiceRemoteBinder.aild
// IAppServiceRemoteBinder.aidl
package com.hqyj.dev.aidltest;
// Declare any non-default types here with import statements
import com.hqyj.dev.aidltest.IRemoteServiceCallback;
interface IAppServiceRemoteBinder {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt,
long aLong,
boolean aBoolean, float aFloat,
double aDouble, String aString);
void setData(String data);
void registerCallback(IRemoteServiceCallback cb);
void unregisterCallback(IRemoteServiceCallback cb);
}
IRemoteServiceCallback.aild
// IRemoteServiceCallback.aidl
package com.hqyj.dev.aidltest;
// Declare any non-default types here with import statements
interface IRemoteServiceCallback {
/**
* return from server
*/
void vlueChanged(String value);
}
And in AndroidManifest.xml, this Server decleared as below:
AndroidManifest.xml
<service
android:name="com.hqyj.dev.aidltest.AppService"
android:enabled="true"
android:exported="true"
android:process=":remote">
</service>
And then, the in second module, copies all these aidl files with package name, as below:
And in MainActivity in anotherapp, complete as below:
package com.hqyj.dev.anotherapp;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.hqyj.dev.aidltest.IAppServiceRemoteBinder;
import com.hqyj.dev.aidltest.IRemoteServiceCallback;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener, ServiceConnection {
private final String TAG = MainActivity.class.getSimpleName();
private Intent intent;
private IAppServiceRemoteBinder binder;
private int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_start).setOnClickListener(this);
findViewById(R.id.btn_stop).setOnClickListener(this);
findViewById(R.id.btn_set).setOnClickListener(this);
intent = new Intent();
intent.setComponent(new
ComponentName("com.hqyj.dev.aidltest",
"com.hqyj.dev.aidltest.AppService"));
}
#SuppressLint("DefaultLocale")
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
bindService(intent, this,
Context.BIND_AUTO_CREATE);
break;
case R.id.btn_set:
if (binder != null) {
try {
binder.setData(
String.format("the %d times",
++count));
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
case R.id.btn_stop:
try {
binder.unregisterCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
unbindService(this);
break;
}
}
#Override
public void onServiceConnected(
ComponentName name, IBinder service) {
binder =
IAppServiceRemoteBinder.Stub.asInterface(service);
Log.d(TAG, "onServiceConnected: " + 1);
try {
binder.registerCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
#Override
public void onServiceDisconnected(ComponentName name) {
}
private IRemoteServiceCallback.Stub callback =
new IRemoteServiceCallback.Stub() {
#Override
public void
vlueChanged(String value) throws RemoteException {
Log.e(TAG, "vlueChanged: " + value);
}
};
}
As you see, I called the remote service by using bindService();
It works well, when I push these two apps into an emulator which using Android 7.0 as platform.
But
When I push these app into an real device(using Android 6.0), the flowing mistake happened:
AIDL failed!!
Why??
I'm trying to change the value of 'video_mp' attribute. But is null from 'logVideoSizeChange' function.
Which is the correct way to declare the video_mp attribute to have access from logVideoSizeChange function?
Source:
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import omlBasePackage.OMLBase;
import omlBasePackage.OMLMPFieldDef;
import omlBasePackage.OMLTypes;
import omlBasePackage.OmlMP;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.Binder;
import android.util.Log;
public class LogService extends Service {
private OMLBase oml;
public static OmlMP video_mp;
public static boolean logServiceIsRunning = false;
static SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyyHH.mm.ss.SSS");
private static String timestamp;
private static String resolution = "";
IBinder mBinder = new LocalBinder();
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public LogService getServerInstance() {
return LogService.this;
}
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
new SetupOml().execute("startExperiment");
logServiceIsRunning = true;
return START_STICKY;
}
private class SetupOml extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... experimentName) {
OMLBase oml = new OMLBase("Player", "player-exp", "exoplayer", "tcp:xx.xx.xx.xx:3003");
ArrayList<OMLMPFieldDef> videoMp = new ArrayList<OMLMPFieldDef>();
videoMp.add(new OMLMPFieldDef("timestamp", OMLTypes.OML_STRING_VALUE));
videoMp.add(new OMLMPFieldDef("width", OMLTypes.OML_STRING_VALUE));
videoMp.add(new OMLMPFieldDef("height", OMLTypes.OML_STRING_VALUE));
OmlMP video_mp = new OmlMP(videoMp);
// Add schema
oml.addmp("video", video_mp);
oml.start();
Log.d("[LogService]", "OML Server started");
return null;
}
}
#Override
public void onDestroy() {
if (oml != null) oml.close();
}
public static void logVideoSizeChange() {
Log.d("[LogService]", "video_mp: " + String.valueOf(video_mp));
}
}
Log results:
10-04 21:20:19.926 20578-20578/com.google.android.exoplayer2.demo D/[LogService]: video_mp: null