Accessing widget from android application - java

I am trying to add weather widget into my application. I used this code to find target weather widget:
public class widgetTask extends AsyncTask<Context, Void, AppWidgetHostView> {
final static int APPWIDGET_HOST_ID = 1;
final static String WEATHER_PACKEGE = "accuweather.android";
final static String WEATHER_CLASS = "accuweather.android.widgets.AL_WidgetProvider";
#Override
protected AppWidgetHostView doInBackground(Context... arg0) {
AppWidgetHostView hostView = null;
AppWidgetManager AWmanager = AppWidgetManager.getInstance(arg0[0]);
AppWidgetHost AWHost = new AppWidgetHost(arg0[0], APPWIDGET_HOST_ID);
AppWidgetProviderInfo AWProviderInfo = new AppWidgetProviderInfo();
int AWId = AWHost.allocateAppWidgetId();
List<AppWidgetProviderInfo> AWProviderInfos = new ArrayList<AppWidgetProviderInfo>();
AWProviderInfos = AWmanager.getInstalledProviders();
for(int j = 0; j < AWProviderInfos.size(); j++)
{
if (AWProviderInfos.get(j).provider.getPackageName().equals("com."+WEATHER_PACKEGE) && AWProviderInfos.get(j).provider.getClassName().equals("com."+WEATHER_CLASS))
{
AWProviderInfo = AWProviderInfos.get(j);
break;
}
}
hostView = AWHost.createView(arg0[0], AWId, AWProviderInfo);
hostView.setAppWidget(AWId, AWProviderInfo);
return hostView;
}
}
After that I tried to add AppWidgetHostView into my Activity:
AppWidgetHostView weatherWidget = WT.execute(this).get();
LLWeather.addView(weatherWidget);
It worked but widget showed "Downloading weather data"(internet permissions granted to this activity) and freezed so I cant interact with it.
Any ideas how to fix it and access to widget functions?

Do not use get() on your asynctask call as it will not be async anymore. The result of doInBackground will be available in onPostExecute. In onPostExecute you call LLWeather.addView.

Related

Downloading Image by URL using AsyncTask and using AnimationDrawable class to change images in intervals

Hi guys trying to implement AsyncTask here in my project, there seems to be no error shown by Android Studio either and have debugged to see if the bitmaps are downloaded and yes it is. I dont know what the problem is so here is my code.
My Activity:
public class MainActivity extends AppCompatActivity {
public ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView =findViewById(R.id.justAnImage);
String[] strings = new String[2];
strings[0] = "blah.com/sds.jpg";
strings[1] = "blah.com/sds2.jpg";
new AsyncDownloader(this,imageView).execute(strings);
}
public void StartAnimation(ImageView imageView, Bitmap[] bitmaps)
{
AnimationDrawable animation = new AnimationDrawable();
for (int i=0;i<bitmaps.length;i++) //replaced erroneous code-> (int i=0;i>=bitmaps.length;i++)
{
animation.addFrame(new BitmapDrawable(getApplicationContext().getResources(),bitmaps[i]),1000);
}
animation.setOneShot(false);
imageView.setBackground(animation);
// start the animation!
animation.start();
}
}
My Async downloader class
public class AsyncDownloader extends AsyncTask<String[],Void,Bitmap[]> {
private Bitmap[] bitmapArray;
private ImageView imageView;
MainActivity mainActivity;
public AsyncDownloader(MainActivity mainActivity,ImageView imageView) {
this.imageView = imageView;
bitmapArray = new Bitmap[2];
this.mainActivity = mainActivity;
}
#Override
protected Bitmap[] doInBackground(String[]... strings) {
for(int i=1;i>=0;i--)
{
try {
URL url = new URL(strings[0][i]);
bitmapArray[i] = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (Exception e) {
Log.e("DOWNLOAD ASYNC", e.getMessage());
}
}
return bitmapArray;
}
#Override
protected void onPostExecute(Bitmap[] bitmapArray) {
super.onPostExecute(bitmapArray);
//imageView.setImageBitmap(bitmapArray[0]);
mainActivity.StartAnimation(imageView,bitmapArray);
}
}
I cant find the problem here, debugging is so terrible in Android Studio. If any one could help, i shall be thankful.
EDIT 1: After suggestions from fellow stack guys, it is clear that the animation drawable is not working as desired and nonetheless there is no image showing up on the ImageView control.
EDIT 2: Code works after correcting the loop specified in the answer. Found an alternative to display images after certain interval. The code goes like this :
public void StartAnimation(final ImageView imageView, final Bitmap[] bitmaps)
{
handler = new Handler();
Runnable runnable = new Runnable() {
int i = 0;
public void run() {
imageView.setImageBitmap(bitmaps[i]);
i++;
if (i > bitmaps.length - 1) {
i = 0;
}
handler.postDelayed(this, 1200);
}
};
handler.postDelayed(runnable, 1200);
}
Your code is perfect, except loop condition inside method StartAnimation.
issue is with loop condition which is always false.
To fix the issue change
From
for (int i = 0; i >= bitmaps.length; i++)
To
for (int i = 0; i < bitmaps.length; i++)
use fresco or picosso or glide library
these libraries automatically caching and managing memory.
it's easy to work with picosso and glide but fresco adds more features to developers.

How correctly apply MVP pattern in my sample

I decide to learn about MVP pattern and after look through some articles i want to try it with my current project.
I have choosen one activity and begin to think how i can decouple it according MVP rules. And eventually I don't know how to do it. It seems like a not complicated activity but I don't know
Could please someone adviced me with what I have to start?
Which methods have to be in presenter, witch view have to be left in this current activity and whitch methods have to be in interface?
Just advised me who i supposed to begin.
This is my class
public final class ActivityUserDataScreen extends AppCompatActivity implements InterfaceActivityUserDataScreen{
private static String gender;
private static int inputHeight;
private static int inputWeight;
private TextInputLayout tilUserName;
private int backPressedQ = 0;
private String avatarName;
private static final String MEN = "men";
private static final String WOMEN = "men";
private Context context;
private PresenterActivityUserDataScreen presenter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_user_data_screen);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setSupportActionBar((Toolbar) findViewById(R.id.tool_bar));
context = getApplicationContext();
initNumberPicker();
initVar();
presenter = new PresenterActivityUserDataScreen(this);
}
private void initNumberPicker() {
NumberPicker pickerHeight = (NumberPicker) findViewById(R.id.pickerHeight);
UtilClass.setDividerColor(pickerHeight, UtilClass.getMyColor(context, R.color.ntz_color_yellow));
pickerHeight.setOnValueChangedListener(changeListener);
pickerHeight.setMaxValue(220);
pickerHeight.setMinValue(130);
pickerHeight.setValue(States.HEIGHT_DEFAULT);
NumberPicker pickerWeight = (NumberPicker) findViewById(R.id.pickerWeight);
UtilClass.setDividerColor(pickerWeight, UtilClass.getMyColor(context, R.color.ntz_color_yellow));
pickerWeight.setOnValueChangedListener(changeListener);
pickerWeight.setMaxValue(120);
pickerWeight.setMinValue(35);
pickerWeight.setValue(States.WEIGHT_DEFAULT);
}
private void initVar() {
tilUserName = (TextInputLayout) findViewById(R.id.tilUserName);
SwitchButton switchButton = (SwitchButton) findViewById(R.id.sb_custom);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
gender = WOMEN;
}else {
gender = MEN;
}
}
});
EditText etAvatarName = (EditText) findViewById(R.id.etAvatarName);
etAvatarName.setText(getResources().getString(R.string.avatar));
}
private NumberPicker.OnValueChangeListener changeListener = new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
switch (picker.getId()) {
case R.id.pickerHeight:
inputHeight = newVal;
break;
case R.id.pickerWeight:
inputWeight = newVal;
break;
}
}
};
#Override
public final void onBackPressed() {
UtilClass.processClick(context);
if (backPressedQ == 1) {
backPressedQ = 0;
super.onBackPressed();
overridePendingTransition(R.anim.open_main, R.anim.close_next);
} else {
backPressedQ++;
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();
}
//Обнуление счётчика через 5 секунд
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
backPressedQ = 0;
}
}, 5000);
}
public final void goNext(View view) {
UtilClass.processClick(context);
EditText editText = tilUserName.getEditText();
Editable editable = null;
if (editText != null) {
editable = editText.getText();
}
if (editable != null) {
avatarName = editable.toString();
}
if (!isValidAvatarName()) return;
saveUserData();
MetadataSaver saver = new MetadataSaver(context);
saver.saveFirstUserInfo();
saver.saveDeviceInfo();
PreferencesHelper.savePref(context, States.STILL_NOT_FINISH, true);
UtilClass.goToNextActivity(ActivityUserDataScreen.this, ActivityVideo.class);
}
private void saveUserData(){
saveAvatarGender();
saveAvatarHeight();
saveAvatarWeight();
saveAvatarName();
}
private void saveAvatarGender(){
if (gender == null){
gender = MEN;
}
PreferencesHelper.savePref(context, States.AVATAR_GENDER, gender);
}
private boolean isValidAvatarName() {
if (UtilClass.isTextEmpty(avatarName)) {
tilUserName.setErrorEnabled(true);
tilUserName.setError(getResources().getString(R.string.fill_your_avatar_name));
return false;
}
if (avatarName.contains(" ")) {
avatarName = avatarName.replace(" ", "");
}
if (!UtilClass.isLatinAlphabet(avatarName)) {
tilUserName.setErrorEnabled(true);
tilUserName.setError(getResources().getString(R.string.avatar_name_in_english));
return false;
}
if (!UtilClass.isNameFree(context, avatarName)) {
tilUserName.setErrorEnabled(true);
tilUserName.setError(getResources().getString(R.string.avatar_name_already_in_use));
return false;
}
return true;
}
private void saveAvatarHeight() {
int result;
if (inputHeight == 0) {
result = States.HEIGHT_DEFAULT;
} else {
result = inputHeight;
}
PreferencesHelper.savePref(context, States.AVATAR_HEIGHT, result);
}
private void saveAvatarWeight() {
int result;
if (inputWeight == 0) {
result = States.WEIGHT_DEFAULT;
} else {
result = inputWeight;
}
PreferencesHelper.savePref(context, States.AVATAR_WEIGHT, result);
}
private void saveAvatarName() {
PreferencesHelper.savePref(context, States.AVATAR_NAME, avatarName);
}
public final void switchManWoman(View view) {
UtilClass.processClick(context);
}
}
Thanks in advance!
The things to take into account are:
The view needs to be as dumb as possible. Think of it as an executor of the commands given by the presenter, and reporter to the presenter of all the stuff that happened on the UI. The interface should provide methods like "display this text", and / or calling presenter's methods like "the button was clicked".
the presenter is the one in command. It drives your view behaviour and reacts to the inputs coming from the view itself. Ideally, it should abstract from anything Android related, in this way you can test the behaviour inside vanilla tests.
Google has published a collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
To begin, very usefull to you to understand how this one works. And adapt to your sample.
[...] This sample is the base for many of the variants. It showcases a simple implementation of the Model-View-Presenter pattern with no architectural frameworks. It uses manual dependency injection to provide a repository with local and remote data sources. Asynchronous tasks are handled with callbacks [...]
I highly recommend reading this article on medium: https://medium.com/#tinmegali/model-view-presenter-mvp-in-android-part-1-441bfd7998fe#.f4yiylrwa .
In essence, all things related to the android SDK should be put in your "view" (and occasionally your model), which will usually be a fragment or activity. Figuring out the difference between your model and presenter will be more up to you, however, you can think about your presenter as the thing that makes program logic decisions based on inputs to your application. Often, the mvp pattern is used in Android development to try to get around rotation and activity recreation issues so you may have luck using a static presenter for a small sample application.
Best of luck!

onClick event managed from a different class with callback

I am pretty new at android/java development and I am trying to process onClick events in an array of view from a "library" class but I can't figure out how it should work.
What I have so far is :
For the library :
public class WordArea {
private ImageView[] imageViewArray;
public boolean createWordTemplate(Activity myActivity, int layoutId, int numberOfLetters, int[] imageArray) {
imageViewArray = new ImageView[10];
// .....
for (int i = 0; i < 10; i++) {
// ...
imageViewArray[i].setClickable(true);
layout.addView(imageViewArray[i], params);
imageViewArray[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
thisIsWhereIwouldLikeTheCallbackToWork( v );
}
});
Then in my activity class I am defining objects like this :
private WordArea myWordArea = new WordArea();
And I would like to define in the activity class the method to be called by the setOnClickListener
Can anyone please help me with the code I should put in the library and in the activity class to make it work ?

Java [Android] - accessing instance variable of an object array in child class

I am quite a beginner and trying to access the instance variable of an object array in the child class but all I get is the initialized values instead of updated values. It is a little more complex but making it simple, the code can be put this way.
Seed.java
public class Seed {
int weight = 0;
}
Apple.java
public class Apple {
public Seed[] seed = new Seed[10];
}
MainActivity.java
Main Activity {
public static Apple[] apple = new Apple[2];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
apple[0] = new Apple();
for (int i = 0; i < 10; i++)
apple[0].seed[i] = new seed();
assign();
//and here new activity childActivity starts
}
public void assign() {
for(int i =0; i < 10; i++)
apple[0].seed[i].weight = 10;
}
}
ChildActivity.java
ChildActivity extends mainActivity {
//display a layout with a button
//upon button click
display();
public void display() {
//output to textview
String.valueOf(apple[0].seed[0].weight);
}
gives me output of 0 instead of 10. I am not able to figure out whats wrong. I checked the values are being assigned properly in the mainActivity. I get no errors or crashes. Thanks for the help!

Get ImageView drawable ID and change it with AsyncTask

What I want to do: get the id of the src of an ImageView, compare it to the ids of two drawables, and swap them, using AsyncTask (just because I want to understand how it works).
I've read similar questions here, and so far this is what I've got:
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.img);
Integer integer = (Integer) image.getTag();
}
private class cambiarImagen extends AsyncTask<Integer, Integer, Integer> {
protected void onPreExecute() {
ImageView image = (ImageView) findViewById(R.id.img);
Integer integer = (Integer) image.getTag();
int img1 = R.drawable.zapato;
int img2 = R.drawable.zapatod;
}
#Override
protected Integer doInBackground(Integer... values) {
// parte logica
int num = values[0];
int zapato = values[1];
int zapatod = values[2];
if (num == zapato) {
num = zapatod;
} else if (num == zapatod) {
num = zapato;
}
return num;
}
protected Void onPostExecute(Integer... values) {
int num = values[0];
ImageView image = (ImageView) findViewById(R.id.img);
image.setTag(num);
return null;
}
}
Of course this doesn't work.
1. I don't understand how to get the id of the drawable that ImageView has as its src.
2. I don't understand how the params are passed in AsyncTask; onPreExecute should receive the UI stuff, doInbackground should receive it to compare it and return the drawable int that should be set to the ImageView, and onPreExecute should set it to the ImageView.
I don't understand how to get the id of the drawable that ImageView has as its src.
I haven't had to do this so may not work but you should be able to use
imageView.getDrawable().getId();
I don't understand how the params are passed in AsyncTask;
Whatever you pass in task.execute() is received by doInBackground(). If you call publishProgress() then whatever params are sent there are received by onProgressUpdate(). And the data returned in doInBackground() is received by onPostExecute().
AsyncTask, just so you know, shouldn't be needed for this but I know you said you wanted to learn how to use it. I was a little confused on exactly what specifically you were having trouble with besides these two things so please elaborate if I missed something.
ImageView Docs
AsyncTask Docs
AsyncTask Example (in case it can be helpful)
You should do other task if you like to get to learn ASyncTask.
I would have done a dialog with progress bar or something instead if i wanted to learn ASyncTask.
edit:
As Samus Arin comment on the main post about you should always have track on which image that you are showing. so instead use something like
if(currentImage == R.Drawable.image1){
image.setImageResource(R.Drawable.image2);
}else{
image.setImageResource(R.Drawable.image1);
}
For what it's worth, take a look at what I'm doing with an AsyncTask, maybe it'll give ya some ideas.
This is Monodroid/C# code, not raw Java/Android (but the syntax is extremely close). As such, inner-classes do not get an implicit reference to their containing object, and so I pass one in (called outer in the constructor). I chose to name it "_" as a lexicographical extension to .NET's _member naming convention for private data members.
public class MonthChangeTask : AsyncTask
{
private CalendarView _; // outer class
private DateTime _newMonth;
private bool _refreshInspectionRecordsRemote;
private bool _changingInspector;
private bool _todayButtonPressed;
private Android.App.ProgressDialog _progressDialog;
private IMXController _controller;
private Dictionary<string, string> _paramz;
private DateTime _newSelectedDate;
public MonthChangeTask( CalendarView outer, DateTime newMonth, bool changingInspector, bool refreshInspectionRecordsRemote, bool todayButtonPressed )
{
_ = outer;
_newMonth = newMonth;
_changingInspector = changingInspector;
_refreshInspectionRecordsRemote = refreshInspectionRecordsRemote;
_todayButtonPressed = todayButtonPressed;
}
protected override void OnPreExecute()
{
base.OnPreExecute();
_progressDialog = Android.App.ProgressDialog.Show( _ , "", "Loading Inspections...");
_newSelectedDate = _._calendar.SetMonth(new DateTime(_newMonth.Year, _newMonth.Month, 1));
AppSettingService.SetCalendarDate(_newMonth);
_paramz = new Dictionary<string, string>();
string target = MD.MxNAVIGATION.CONTROLLER.CALENDAR._name;
string action = MD.MxNAVIGATION.CONTROLLER.ACTION.GET;
string command = _refreshInspectionRecordsRemote
? ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsRemote).ToString()
: ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsLocal).ToString();
string url = target + "/" + action + "/" + command;
_controller = MXContainer.Instance.GetController(url, ref _paramz);
}
protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] #params)
{
if ( _paramz == null )
{
Log.Info(FIDB.TAG_APP, "MonthChangeTask.DoInBackground(): paramz = NULL");
}
else
{
_controller.Load( _paramz );
}
return true;
}
protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);
_progressDialog.Dismiss();
_.Model = (CalendarVM)_controller.GetModel();
if (_changingInspector)
{
_._calendar.PermitSwitch = _.Model.Buttons.PermitsVisible;
_._calendar.ComplaintSwitch = _.Model.Buttons.ComplaintsVisible;
_._calendar.ProjectSwitch = _.Model.Buttons.ProjectsVisible;
_._calendar.PeriodicInspectionSwitch = _.Model.Buttons.PeriodicInspectionsVisible;
}
_.UpdateCalendar(_.Model.Inspections);
if( _todayButtonPressed )
{
_._calendar.SelectedDate = _._calendar.CurrentDate;
}
else
{
_._calendar.SelectedDate = _newSelectedDate;
}
_._calendar.Invalidate();
AppSettingService.SetCalendarDate( _._calendar.SelectedDate );
if ( _.Model.IsParcelCacheDownloading )
{
AnimationTask task = new AnimationTask( _ );
task.Execute( new Java.Lang.Object[1] );
}
}
}

Categories