Android Open Source - esr-scanner History Fragment






From Project

Back to project page esr-scanner.

License

The source code is released under:

Apache License

If you think the Android project esr-scanner 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

package ch.luklanis.esscan.history;
// w w  w.ja  v a2  s .co  m
import android.app.Activity;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.Comparator;

import ch.luklanis.esscan.R;
import ch.luklanis.esscan.dialogs.CancelOkDialog;

public class HistoryFragment extends ListFragment {

    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    private HistoryCallbacks historyCallbacks;
    private int activatedPosition = ListView.INVALID_POSITION;

    public interface HistoryCallbacks {
        public void selectTopInTwoPane();

        public void onItemSelected(int oldPosition, int newPosition);

        public int getPositionToActivate();
    }

    private HistoryManager historyManager;
    private HistoryItemAdapter adapter;

    private HistoryFragment self;

    private boolean listIsEmpty;

    private static HistoryCallbacks sDummyCallbacks = new HistoryCallbacks() {
        @Override
        public void onItemSelected(int oldPosition, int newPosition) {
        }

        @Override
        public int getPositionToActivate() {
            return -1;
        }

        @Override
        public void selectTopInTwoPane() {
        }
    };

    protected static final Comparator<HistoryItem> historyComparator = new Comparator<HistoryItem>() {

        @Override
        public int compare(HistoryItem lhs, HistoryItem rhs) {
            if (rhs.getResult() == null) {
                if (lhs.getResult() == null) {
                    return 0;
                }

                return 1;
            }

            if (lhs.getResult() == null) {
                return -1;
            }

            return (Long.valueOf(rhs.getResult().getTimestamp())).compareTo(lhs.getResult()
                    .getTimestamp());
        }
    };

    public HistoryFragment() {
        listIsEmpty = true;
    }

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

        historyManager = new HistoryManager(getActivity().getApplicationContext());
        self = this;

        adapter = null;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {

        if (listIsEmpty) {
            return;
        }

        super.onListItemClick(listView, view, position, id);

        int oldPosition = activatedPosition;
        activatedPosition = position;

        historyCallbacks.onItemSelected(oldPosition, position);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }

        ListView listview = getListView();
        registerForContextMenu(listview);
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position,
                                           long id) {

                activatedPosition = position;

                new CancelOkDialog(R.string.msg_confirm_delete_payment_title,
                        R.string.msg_confirm_delete_message).setOkClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        historyManager.deleteHistoryItem(activatedPosition);
                        historyCallbacks.selectTopInTwoPane();

                        GetHistoryAsyncTask async = new GetHistoryAsyncTask(self, historyManager);
                        async.execute();
                    }
                }).show(getFragmentManager(), "HistoryFragment.onViewCreated");
                return true;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        GetHistoryAsyncTask async = new GetHistoryAsyncTask(this, historyManager);
        async.execute();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof HistoryCallbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        historyCallbacks = (HistoryCallbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        historyCallbacks = sDummyCallbacks;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (activatedPosition != ListView.INVALID_POSITION) {
            outState.putInt(STATE_ACTIVATED_POSITION, activatedPosition);
        }
    }

    public void setActivateOnItemClick(boolean activateOnItemClick) {
        getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
    }

    public void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(activatedPosition, false);
        } else {
            getListView().setItemChecked(position, true);
        }

        activatedPosition = position;
    }

    public void updatePosition(int position, HistoryItem item) {

        HistoryItem listItem = getHistoryItemOnPosition(position);
        if (listItem != null) {
            listItem.update(item);

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });
        }
    }

    public HistoryItem getHistoryItemOnPosition(int position) {

        if (adapter != null && adapter.getCount() > position) {
            return adapter.getItem(position);
        }

        return null;
    }

    public void dataChanged() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    public void setHistoryItemAdapter(HistoryItemAdapter adapter) {

        if (adapter.isEmpty()) {
            adapter.add(HistoryItem.Builder.createEmptyInstance());
            listIsEmpty = true;
        } else {
            listIsEmpty = false;
        }

        this.adapter = adapter;

        try {
            setListAdapter(this.adapter);
            setListShown(true);

            this.adapter.notifyDataSetInvalidated();

            int position = historyCallbacks.getPositionToActivate();
            setActivatedPosition(position);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public HistoryItemAdapter getHistoryItemAdapter() {
        return adapter;
    }

    public int getActivatedPosition() {
        return activatedPosition;
    }
}




Java Source Code List

ch.luklanis.android.common.PlatformSupportManager.java
ch.luklanis.android.common.executor.AsyncTaskExecInterface.java
ch.luklanis.android.common.executor.AsyncTaskExecManager.java
ch.luklanis.android.common.executor.DefaultAsyncTaskExecInterface.java
ch.luklanis.android.common.executor.HoneycombAsyncTaskExecInterface.java
ch.luklanis.esscan.BeepManager.java
ch.luklanis.esscan.CaptureActivityHandler.java
ch.luklanis.esscan.CaptureActivity.java
ch.luklanis.esscan.DecodeHandler.java
ch.luklanis.esscan.DecodeThread.java
ch.luklanis.esscan.EsrBaseActivity.java
ch.luklanis.esscan.FinishListener.java
ch.luklanis.esscan.HelpActivity.java
ch.luklanis.esscan.IBase.java
ch.luklanis.esscan.Intents.java
ch.luklanis.esscan.LuminanceSource.java
ch.luklanis.esscan.OcrCharacterHelper.java
ch.luklanis.esscan.OcrInitAsyncTask.java
ch.luklanis.esscan.OcrRecognizeAsyncTask.java
ch.luklanis.esscan.OcrResultFailure.java
ch.luklanis.esscan.OcrResultText.java
ch.luklanis.esscan.OcrResult.java
ch.luklanis.esscan.PlanarYUVLuminanceSource.java
ch.luklanis.esscan.PreferencesActivity.java
ch.luklanis.esscan.ViewfinderView.java
ch.luklanis.esscan.camera.AutoFocusManager.java
ch.luklanis.esscan.camera.CameraConfigurationManager.java
ch.luklanis.esscan.camera.CameraManager.java
ch.luklanis.esscan.camera.PreviewCallback.java
ch.luklanis.esscan.camera.exposure.DefaultExposureInterface.java
ch.luklanis.esscan.camera.exposure.ExposureInterface.java
ch.luklanis.esscan.camera.exposure.ExposureManager.java
ch.luklanis.esscan.camera.exposure.FroyoExposureInterface.java
ch.luklanis.esscan.camera.open.DefaultOpenCameraInterface.java
ch.luklanis.esscan.camera.open.GingerbreadOpenCameraInterface.java
ch.luklanis.esscan.camera.open.OpenCameraInterface.java
ch.luklanis.esscan.camera.open.OpenCameraManager.java
ch.luklanis.esscan.codesend.Crypto.java
ch.luklanis.esscan.codesend.ESRSenderHttp.java
ch.luklanis.esscan.codesend.IEsrSender.java
ch.luklanis.esscan.dialogs.BankProfileDialogFragment.java
ch.luklanis.esscan.dialogs.BankProfileListDialog.java
ch.luklanis.esscan.dialogs.CancelOkDialog.java
ch.luklanis.esscan.dialogs.OkDialog.java
ch.luklanis.esscan.dialogs.OptionalOkDialog.java
ch.luklanis.esscan.history.BankProfile.java
ch.luklanis.esscan.history.DBHelper.java
ch.luklanis.esscan.history.GetHistoryAsyncTask.java
ch.luklanis.esscan.history.HistoryActivity.java
ch.luklanis.esscan.history.HistoryExportUpdateAsyncTask.java
ch.luklanis.esscan.history.HistoryFragment.java
ch.luklanis.esscan.history.HistoryItemAdapter.java
ch.luklanis.esscan.history.HistoryItem.java
ch.luklanis.esscan.history.HistoryManager.java
ch.luklanis.esscan.history.PsDetailActivity.java
ch.luklanis.esscan.history.PsDetailFragment.java
ch.luklanis.esscan.ime.ScannerIME.java
ch.luklanis.esscan.paymentslip.DTAFileCreator.java
ch.luklanis.esscan.paymentslip.EsIbanResult.java
ch.luklanis.esscan.paymentslip.EsIbanValidation.java
ch.luklanis.esscan.paymentslip.EsrResult.java
ch.luklanis.esscan.paymentslip.EsrValidation.java
ch.luklanis.esscan.paymentslip.PsResult.java
ch.luklanis.esscan.paymentslip.PsValidation.java