Android Open Source - sthlmtraveling Deviations Activity






From Project

Back to project page sthlmtraveling.

License

The source code is released under:

Apache License

If you think the Android project sthlmtraveling listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2009 Johan Nilsson <http://markupartist.com>
 */*from w  ww .j  av a 2  s.  co m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.markupartist.sthlmtraveling;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.format.Time;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.view.WindowManager.BadTokenException;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.markupartist.sthlmtraveling.provider.deviation.Deviation;
import com.markupartist.sthlmtraveling.provider.deviation.DeviationStore;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DeviationsActivity extends BaseListActivity {
    private static final String STATE_GET_DEVIATIONS_IN_PROGRESS =
        "com.markupartist.sthlmtraveling.getdeviations.inprogress";
    public static final String DEVIATION_FILTER_ACTION =
        "com.markupartist.sthlmtraveling.action.DEVIATION_FILTER";
    static String TAG = "DeviationsActivity";

    private static final int DIALOG_GET_DEVIATIONS_NETWORK_PROBLEM = 1;

    private GetDeviationsTask mGetDeviationsTask;
    private ArrayList<Deviation> mDeviationsResult;
    private LinearLayout mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        setContentView(R.layout.deviations_list);

        registerScreen("Deviations");

        mProgress = (LinearLayout) findViewById(R.id.search_progress);
        mProgress.setVisibility(View.GONE);

        initActionBar();

        loadDeviations();
        registerForContextMenu(getListView());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.actionbar_deviations, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.actionbar_item_refresh:
            mGetDeviationsTask = new GetDeviationsTask();
            mGetDeviationsTask.execute();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void loadDeviations() {
        @SuppressWarnings("unchecked")
        final ArrayList<Deviation> result =
            (ArrayList<Deviation>) getLastNonConfigurationInstance();
        if (result != null) {
            fillData(result);
        } else {
            mGetDeviationsTask = new GetDeviationsTask();
            mGetDeviationsTask.execute();
        }
    }

    private void fillData(ArrayList<Deviation> result) {
        TextView emptyResultView = (TextView) findViewById(R.id.deviations_empty_result);

        // TODO: Needs to be moved later on...
        if (DEVIATION_FILTER_ACTION.equals(getIntent().getAction())) {
            SharedPreferences sharedPreferences =
                PreferenceManager.getDefaultSharedPreferences(this);
            String filterString = sharedPreferences.getString("notification_deviations_lines_csv", "");
            ArrayList<Integer> triggerFor = DeviationStore.extractLineNumbers(filterString, null);
            result = DeviationStore.filterByLineNumbers(result, triggerFor);
        }

        if (result.isEmpty()) {
            Log.d(TAG, "is empty");
            emptyResultView.setVisibility(View.VISIBLE);
            return;
        }
        emptyResultView.setVisibility(View.GONE);

        SimpleAdapter deviationAdapter = createAdapter(result);

        mDeviationsResult = result;

        setListAdapter(deviationAdapter);
    }

    private SimpleAdapter createAdapter(ArrayList<Deviation> deviations) {
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();

        Time now = new Time();
        now.setToNow();
        for (Deviation deviation : deviations) {
            Time created = deviation.getCreated();

            Map<String, String> map = new HashMap<String, String>();
            map.put("header", deviation.getHeader());
            map.put("details", deviation.getDetails());
            map.put("created", created.format("%F %R"));
            map.put("scope", deviation.getScope());
            list.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(this, list, 
                R.layout.deviations_row,
                new String[] { "header", "details", "created", "scope"},
                new int[] { 
                    R.id.deviation_header,
                    R.id.deviation_details,
                    R.id.deviation_created,
                    R.id.deviation_scope
                }
        );

        adapter.setViewBinder(new ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
                switch (view.getId()) {
                case R.id.deviation_header:
                case R.id.deviation_details:
                case R.id.deviation_created:
                case R.id.deviation_scope:
                    ((TextView)view).setText(textRepresentation);
                    return true;
                }
                return false;
            }
        });

        return adapter;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(R.string.share_label);
    }

    @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo =
            (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Deviation deviation = mDeviationsResult.get(menuInfo.position);
        share(deviation.getHeader(), deviation.getDetails());
        return true;
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return mDeviationsResult;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveGetDeviationsTask(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        restoreLocalState(savedInstanceState);
    }

    /**
     * Restores any local state, if any.
     * @param savedInstanceState the bundle containing the saved state
     */
    private void restoreLocalState(Bundle savedInstanceState) {
        restoreGetDeviationsTask(savedInstanceState);
    }

    /**
     * Cancels the {@link GetDeviationsTask} if it is running.
     */
    private void onCancelGetDeviationsTask() {
        if (mGetDeviationsTask != null &&
                mGetDeviationsTask.getStatus() == AsyncTask.Status.RUNNING) {
            Log.i(TAG, "Cancels GetDeviationsTask.");
            mGetDeviationsTask.cancel(true);
            mGetDeviationsTask= null;
        }
    }

    /**
     * Restores the {@link GetDeviationsTask}.
     * @param savedInstanceState the saved state
     */
    private void restoreGetDeviationsTask(Bundle savedInstanceState) {
        if (savedInstanceState.getBoolean(STATE_GET_DEVIATIONS_IN_PROGRESS)) {
            Log.d(TAG, "restoring getDeviationsTask");
            mGetDeviationsTask = new GetDeviationsTask();
            mGetDeviationsTask.execute();
        }
    }

    /**
     * Saves the state of {@link GetDeviationsTask}.
     * @param outState
     */
    private void saveGetDeviationsTask(Bundle outState) {
        final GetDeviationsTask task = mGetDeviationsTask;
        if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
            Log.d(TAG, "saving GetDeviationsState");
            task.cancel(true);
            mGetDeviationsTask = null;
            outState.putBoolean(STATE_GET_DEVIATIONS_IN_PROGRESS, true);
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
        case DIALOG_GET_DEVIATIONS_NETWORK_PROBLEM:
            return DialogHelper.createNetworkProblemDialog(this, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mGetDeviationsTask = new GetDeviationsTask();
                    mGetDeviationsTask.execute();
                }
            });
        }
        return null;
    }

    /**
     * Show progress dialog.
     */
    private void showProgress() {
        setSupportProgressBarIndeterminateVisibility(true);
    }

    /**
     * Dismiss the progress dialog.
     */
    private void dismissProgress() {
        setSupportProgressBarIndeterminateVisibility(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        onCancelGetDeviationsTask();

        dismissProgress();
    }

    @Override
    public boolean onSearchRequested() {
        Intent i = new Intent(this, StartActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        return true;
    }

    /**
     * Background job for getting {@link Deviation}s.
     */
    private class GetDeviationsTask extends AsyncTask<Void, Void, ArrayList<Deviation>> {
        private boolean mWasSuccess = true;

        @Override
        public void onPreExecute() {
            showProgress();
        }

        @Override
        protected ArrayList<Deviation> doInBackground(Void... params) {
            try {
                DeviationStore deviationStore = new DeviationStore();
                return deviationStore.getDeviations(DeviationsActivity.this);
            } catch (IOException e) {
                mWasSuccess = false;
                return null;
            }
        }

        @Override
        protected void onPostExecute(ArrayList<Deviation> result) {
            dismissProgress();

            if (mWasSuccess) {
                fillData(result);
            } else {
                try {
                    showDialog(DIALOG_GET_DEVIATIONS_NETWORK_PROBLEM);
                } catch (BadTokenException e) {
                    Log.w(TAG, "Caught BadTokenException when trying to show network error dialog.");
                }
            }
        }
    }

    /**
     * Share a {@link Deviation} with others.
     * @param subject the subject
     * @param text the text
     */
    public void share(String subject,String text) {
        final Intent intent = new Intent(Intent.ACTION_SEND);

        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, text);

        startActivity(Intent.createChooser(intent, getText(R.string.share_label)));
    }
}




Java Source Code List

com.markupartist.sthlmtraveling.AboutActivity.java
com.markupartist.sthlmtraveling.AllTests.java
com.markupartist.sthlmtraveling.AppConfig.java
com.markupartist.sthlmtraveling.AutoCompleteStopAdapter.java
com.markupartist.sthlmtraveling.BaseActivity.java
com.markupartist.sthlmtraveling.BaseFragmentActivity.java
com.markupartist.sthlmtraveling.BaseFragment.java
com.markupartist.sthlmtraveling.BaseListActivity.java
com.markupartist.sthlmtraveling.BaseListFragmentActivity.java
com.markupartist.sthlmtraveling.BaseListFragment.java
com.markupartist.sthlmtraveling.BasePreferenceActivity.java
com.markupartist.sthlmtraveling.ChangeRouteTimeActivity.java
com.markupartist.sthlmtraveling.DepartureAdapter.java
com.markupartist.sthlmtraveling.DeparturesActivity.java
com.markupartist.sthlmtraveling.DeviationDetailActivity.java
com.markupartist.sthlmtraveling.DeviationsActivity.java
com.markupartist.sthlmtraveling.DialogHelper.java
com.markupartist.sthlmtraveling.FavoritesFragment.java
com.markupartist.sthlmtraveling.MultipleListAdapter.java
com.markupartist.sthlmtraveling.MyApplication.java
com.markupartist.sthlmtraveling.MyLocationManager.java
com.markupartist.sthlmtraveling.NearbyActivity.java
com.markupartist.sthlmtraveling.PlannerFragmentActivity.java
com.markupartist.sthlmtraveling.PlannerFragment.java
com.markupartist.sthlmtraveling.PointOnMapActivity.java
com.markupartist.sthlmtraveling.RouteDetailActivity.java
com.markupartist.sthlmtraveling.RouteParserTest.java
com.markupartist.sthlmtraveling.RoutesActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragmentActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragment.java
com.markupartist.sthlmtraveling.SectionedAdapter.java
com.markupartist.sthlmtraveling.SettingsActivity.java
com.markupartist.sthlmtraveling.StartActivity.java
com.markupartist.sthlmtraveling.TrafficStatusFragment.java
com.markupartist.sthlmtraveling.ViewOnMapActivity.java
com.markupartist.sthlmtraveling.provider.FavoritesDbAdapter.java
com.markupartist.sthlmtraveling.provider.HistoryDbAdapter.java
com.markupartist.sthlmtraveling.provider.JourneysProvider.java
com.markupartist.sthlmtraveling.provider.PlacesProvider.java
com.markupartist.sthlmtraveling.provider.TransportMode.java
com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationNotificationDbAdapter.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationStore.java
com.markupartist.sthlmtraveling.provider.deviation.Deviation.java
com.markupartist.sthlmtraveling.provider.planner.JourneyQuery.java
com.markupartist.sthlmtraveling.provider.planner.Planner.java
com.markupartist.sthlmtraveling.provider.site.Site.java
com.markupartist.sthlmtraveling.provider.site.SitesStore.java
com.markupartist.sthlmtraveling.receivers.OnAlarmReceiver.java
com.markupartist.sthlmtraveling.receivers.OnBootReceiver.java
com.markupartist.sthlmtraveling.service.DataMigrationService.java
com.markupartist.sthlmtraveling.service.DeviationService.java
com.markupartist.sthlmtraveling.service.WakefulIntentService.java
com.markupartist.sthlmtraveling.ui.view.DelayAutoCompleteTextView.java
com.markupartist.sthlmtraveling.ui.view.LineSegment.java
com.markupartist.sthlmtraveling.ui.view.SmsTicketDialog.java
com.markupartist.sthlmtraveling.ui.view.TripView.java
com.markupartist.sthlmtraveling.utils.Analytics.java
com.markupartist.sthlmtraveling.utils.BarcodeScannerIntegrator.java
com.markupartist.sthlmtraveling.utils.DateTimeUtil.java
com.markupartist.sthlmtraveling.utils.DisplayMetricsHelper.java
com.markupartist.sthlmtraveling.utils.ErrorReporter.java
com.markupartist.sthlmtraveling.utils.HttpHelper.java
com.markupartist.sthlmtraveling.utils.IntentUtil.java
com.markupartist.sthlmtraveling.utils.LocationUtils.java
com.markupartist.sthlmtraveling.utils.StreamUtils.java
com.markupartist.sthlmtraveling.utils.StringUtils.java
com.markupartist.sthlmtraveling.utils.ViewHelper.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java