Skip to content
Snippets Groups Projects
Commit 4d5b296a authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

updated to remove asycn tasks

parent ddd8b284
Branches
No related tags found
No related merge requests found
......@@ -47,7 +47,9 @@ public class NewFeedTest extends
// 'course.labs.threadslab.MainActivity'
solo.waitForActivity(course.labs.notificationslab.MainActivity.class,
shortDelay);
String msg = getActivity().getString(course.labs.notificationslab.R.string.download_in_progress_string);
assertFalse("'" + msg + " ' is shown!",
solo.searchText(msg));
solo.waitForView(android.R.id.list);
final View listView = solo.getView(android.R.id.list);
......
......@@ -78,173 +78,181 @@ public class DownloaderTaskFragment extends Fragment {
mCallback = null;
}
// TODO: Implement an AsyncTask subclass called DownLoaderTask.
// This class must use the downloadTweets method and the methods called inside it(currently commented
// out). Ultimately, it must also pass newly available data back to
// the hosting Activity using the DownloadFinishedListener interface.
// public class DownloaderTask extends ...
public class DownloaderTask extends AsyncTask<Integer, Void, String[]> {
@Override
protected String[] doInBackground(Integer... resourceIDs) {
return downloadTweets(resourceIDs);
}
// TODO: Uncomment this helper method and put them inside the DownLoaderTask subclass
// Simulates downloading Twitter data from the network
// private String[] downloadTweets(Integer resourceIDS[]) {
// final int simulatedDelay = 2000;
// String[] feeds = new String[resourceIDS.length];
// boolean downLoadCompleted = false;
//
// try {
// for (int idx = 0; idx < resourceIDS.length; idx++) {
// InputStream inputStream;
// BufferedReader in;
// try {
// // Pretend downloading takes a long time
// Thread.sleep(simulatedDelay);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//
// inputStream = mContext.getResources().openRawResource(
// resourceIDS[idx]);
// in = new BufferedReader(new InputStreamReader(inputStream));
//
// String readLine;
// StringBuffer buf = new StringBuffer();
//
// while ((readLine = in.readLine()) != null) {
// buf.append(readLine);
// }
//
// feeds[idx] = buf.toString();
//
// if (null != in) {
// in.close();
// }
// }
//
// downLoadCompleted = true;
// saveTweetsToFile(feeds);
//
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// // Notify user that downloading has finished
// notify(downLoadCompleted);
//
// return feeds;
//
// }
private String[] downloadTweets(Integer resourceIDS[]) {
final int simulatedDelay = 2000;
String[] feeds = new String[resourceIDS.length];
boolean downLoadCompleted = false;
try {
for (int idx = 0; idx < resourceIDS.length; idx++) {
InputStream inputStream;
BufferedReader in;
try {
// Pretend downloading takes a long time
Thread.sleep(simulatedDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
inputStream = mContext.getResources().openRawResource(
resourceIDS[idx]);
in = new BufferedReader(new InputStreamReader(inputStream));
String readLine;
StringBuffer buf = new StringBuffer();
while ((readLine = in.readLine()) != null) {
buf.append(readLine);
}
feeds[idx] = buf.toString();
if (null != in) {
in.close();
}
}
downLoadCompleted = true;
saveTweetsToFile(feeds);
} catch (IOException e) {
e.printStackTrace();
}
// Notify user that downloading has finished
notify(downLoadCompleted);
return feeds;
}
// If necessary, notifies the user that the tweet downloads are
// complete. Sends an ordered broadcast back to the BroadcastReceiver in
// MainActivity to determine whether the notification is necessary.
// private void notify(final boolean success) {
// final Intent restartMainActivityIntent = new Intent(mContext,
// MainActivity.class);
// restartMainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
// // Sends an ordered broadcast to determine whether MainActivity is
// // active and in the foreground. Creates a new BroadcastReceiver
// // to receive a result indicating the state of MainActivity
//
// // The Action for this broadcast Intent is
// // MainActivity.DATA_REFRESHED_ACTION
// // The result, MainActivity.IS_ALIVE, indicates that MainActivity is
// // active and in the foreground.
//
// mContext.sendOrderedBroadcast(new Intent(
// MainActivity.DATA_REFRESHED_ACTION), null,
// new BroadcastReceiver() {
//
// final String failMsg = mContext
// .getString(R.string.download_failed_string);
// final String successMsg = mContext
// .getString(R.string.download_succes_string);
// final String notificationSentMsg = mContext
// .getString(R.string.notification_sent_string);
//
// @Override
// public void onReceive(Context context, Intent intent) {
//
// // TODO: Check whether or not the MainActivity
// // received the broadcast
//
// if () {
//
// // TODO: If not, create a PendingIntent using
// // the
// // restartMainActivityIntent and set its flags
// // to FLAG_UPDATE_CURRENT
//
//
//
// // Uses R.layout.custom_notification for the
// // layout of the notification View. The xml
// // file is in res/layout/custom_notification.xml
//
//
//
// // TODO: Set the notification View's text to
// // reflect whether the download completed
// // successfully (successMsg or failMsg)
//
//
// // TODO: Use the Notification.Builder class to
// // create the Notification. You will have to set
// // several pieces of information. You can use
// // android.R.drawable.stat_sys_warning
// // for the small icon. You should also
// // setAutoCancel(true).
// // To support API Level 26 implement the TODOs in createNotificationChannel
// createNotificationChannel();
//
//
// // TODO: Send the notification
//
// Toast.makeText(mContext, notificationSentMsg,
// Toast.LENGTH_LONG).show();
//
// } else {
// Toast.makeText(mContext,
// success ? successMsg : failMsg,
// Toast.LENGTH_LONG).show();
// }
// }
// }, null, 0, null, null);
// }
// private String channelID = "my_channel_01";
//
// private void createNotificationChannel()
// {
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// // TODO: Create Notification Channel with id channelID,
// // name R.string.channel_name
// // and description R.string.channel_description of high importance
//
// }
// }
// // Saves the tweets to a file
// private void saveTweetsToFile(String[] result) {
// PrintWriter writer = null;
// try {
// FileOutputStream fos = mContext.openFileOutput(
// MainActivity.TWEET_FILENAME, Context.MODE_PRIVATE);
// writer = new PrintWriter(new BufferedWriter(
// new OutputStreamWriter(fos)));
//
// for (String s : result) {
// writer.println(s);
// }
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// if (null != writer) {
// writer.close();
// }
// }
// }
private void notify(final boolean success) {
final Intent restartMainActivityIntent = new Intent(mContext,
MainActivity.class);
restartMainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Sends an ordered broadcast to determine whether MainActivity is
// active and in the foreground. Creates a new BroadcastReceiver
// to receive a result indicating the state of MainActivity
// The Action for this broadcast Intent is
// MainActivity.DATA_REFRESHED_ACTION
// The result, MainActivity.IS_ALIVE, indicates that MainActivity is
// active and in the foreground.
mContext.sendOrderedBroadcast(new Intent(
MainActivity.DATA_REFRESHED_ACTION), null,
new BroadcastReceiver() {
final String failMsg = mContext
.getString(R.string.download_failed_string);
final String successMsg = mContext
.getString(R.string.download_succes_string);
final String notificationSentMsg = mContext
.getString(R.string.notification_sent_string);
@Override
public void onReceive(Context context, Intent intent) {
// TODO: Change the if condition (false) to Check whether or not the MainActivity
// received the broadcast
if (false) {
// TODO: If not, create a PendingIntent using
// the
// restartMainActivityIntent and set its flags
// to FLAG_UPDATE_CURRENT
final PendingIntent pendingIntent = null;
// Uses R.layout.custom_notification for the
// layout of the notification View. The xml
// file is in res/layout/custom_notification.xml
// TODO: Set the notification View's text to
// reflect whether the download completed
// successfully
// TODO: Use the Notification.Builder class to
// create the Notification. You will have to set
// several pieces of information. You can use
// android.R.drawable.stat_sys_warning
// for the small icon. You should also
// setAutoCancel(true).
// To support API level 26, implement the TODOs in createNotificationChannel()
createNotificationChannel();
// TODO: Send the notification and create a toast with notificationSentMsg text
} else {
Toast.makeText(mContext,
success ? successMsg : failMsg,
Toast.LENGTH_LONG).show();
}
}
}, null, 0, null, null);
}
private String channelID = "my_channel_01";
private void createNotificationChannel()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// TODO: Create Notification Channel with id channelID,
// name R.string.channel_name
// and description R.string.channel_description of high importance
}
}
// Saves the tweets to a file
private void saveTweetsToFile(String[] result) {
PrintWriter writer = null;
try {
FileOutputStream fos = mContext.openFileOutput(
MainActivity.TWEET_FILENAME, Context.MODE_PRIVATE);
writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(fos)));
for (String s : result) {
writer.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != writer) {
writer.close();
}
}
}
// Pass newly available data back to hosting Activity
// using the DownloadFinishedListener interface
@Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
if (null != mCallback) {
mCallback.notifyDataRefreshed(result);
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment