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