Example usage for android.view GestureDetector.OnGestureListener GestureDetector.OnGestureListener

List of usage examples for android.view GestureDetector.OnGestureListener GestureDetector.OnGestureListener

Introduction

In this page you can find the example usage for android.view GestureDetector.OnGestureListener GestureDetector.OnGestureListener.

Prototype

GestureDetector.OnGestureListener

Source Link

Usage

From source file:com.juick.android.ThreadActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    JuickAdvancedApplication.maybeEnableAcceleration(this);
    JuickAdvancedApplication.setupTheme(this);
    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    //getSherlock().requestFeature(Window.FEATURE_NO_TITLE);
    final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    super.onCreate(savedInstanceState);
    handler = new Handler();

    Intent i = getIntent();//from ww  w  .j av  a  2s  . c o  m
    mid = (MessageID) i.getSerializableExtra("mid");
    if (mid == null) {
        finish();
    }

    messagesSource = (MessagesSource) i.getSerializableExtra("messagesSource");
    if (sp.getBoolean("fullScreenThread", false)) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }
    setContentView(R.layout.thread);
    /*
            findViewById(R.id.gotoMain).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(ThreadActivity.this, MainActivity.class));
    }
            });
    */
    final View buttons = findViewById(R.id.buttons);
    bSend = (ImageButton) findViewById(R.id.buttonSend);
    bSend.setOnClickListener(this);
    bAttach = (ImageButton) findViewById(R.id.buttonAttachment);
    bAttach.setOnClickListener(this);
    etMessage = (EditText) findViewById(R.id.editMessage);

    if (sp.getBoolean("helvNueFonts", false)) {
        etMessage.setTypeface(JuickAdvancedApplication.helvNue);
        /*
                    TextView oldTitle = (TextView)findViewById(R.id.old_title);
                    oldTitle.setTypeface(JuickAdvancedApplication.helvNue);
        */
    }

    Button cancel = (Button) findViewById(R.id.buttonCancel);
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            doCancel();
        }
    });

    tvReplyTo = (TextView) findViewById(R.id.textReplyTo);
    replyToContainer = (RelativeLayout) findViewById(R.id.replyToContainer);
    setHeight(replyToContainer, 0);
    showThread = (Button) findViewById(R.id.showThread);
    draftsButton = (Button) findViewById(R.id.drafts);
    etMessage.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                etMessage.setHint("");
                setHeight(buttons, ActionBar.LayoutParams.WRAP_CONTENT);
                InputMethodManager inputMgr = (InputMethodManager) getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                inputMgr.toggleSoftInput(0, 0);
            } else {
                etMessage.setHint(R.string.ClickToReply);
                setHeight(buttons, 0);
            }
            //To change body of implemented methods use File | Settings | File Templates.
        }
    });
    draftsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            class Item {
                String label;
                long ts;
                int index;

                Item(String label, long ts, int index) {
                    this.label = label;
                    this.ts = ts;
                    this.index = index;
                }
            }
            final ArrayList<Item> items = new ArrayList<Item>();
            final SharedPreferences drafts = getSharedPreferences("drafts", MODE_PRIVATE);
            for (int q = 0; q < 1000; q++) {
                String msg = drafts.getString("message" + q, null);
                if (msg != null) {
                    if (msg.length() > 50)
                        msg = msg.substring(0, 50);
                    items.add(new Item(msg, drafts.getLong("timestamp" + q, 0), q));
                }
            }
            Collections.sort(items, new Comparator<Item>() {
                @Override
                public int compare(Item item, Item item2) {
                    final long l = item2.ts - item.ts;
                    return l == 0 ? 0 : l > 0 ? 1 : -1;
                }
            });
            CharSequence[] arr = new CharSequence[items.size()];
            for (int i1 = 0; i1 < items.size(); i1++) {
                Item item = items.get(i1);
                arr[i1] = item.label;
            }
            new AlertDialog.Builder(ThreadActivity.this).setItems(arr, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, final int which) {
                    final Runnable doPull = new Runnable() {
                        @Override
                        public void run() {
                            pullDraft(null, drafts, items.get(which).index);
                            updateDraftsButton();
                        }
                    };
                    if (pulledDraft != null
                            && pulledDraft.trim().equals(etMessage.getText().toString().trim())) {
                        // no need to ask, user just looks at the drafts
                        saveDraft(pulledDraftRid, pulledDraftMid, pulledDraftTs, pulledDraft);
                        doPull.run();
                    } else {
                        if (etMessage.getText().toString().length() > 0) {
                            new AlertDialog.Builder(ThreadActivity.this)
                                    .setTitle(getString(R.string.ReplacingText))
                                    .setMessage(getString(R.string.YourTextWillBeReplaced))
                                    .setNegativeButton(android.R.string.ok,
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    doPull.run();
                                                }

                                            })
                                    .setNeutralButton(
                                            getString(pulledDraft != null ? R.string.SaveChangedDraft
                                                    : R.string.SaveDraft),
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    if (pulledDraft != null) {
                                                        saveDraft(pulledDraftRid, pulledDraftMid,
                                                                System.currentTimeMillis(),
                                                                etMessage.getText().toString());
                                                    } else {
                                                        saveDraft(rid, mid.toString(),
                                                                System.currentTimeMillis(),
                                                                etMessage.getText().toString());
                                                    }
                                                    doPull.run();
                                                }
                                            })
                                    .setPositiveButton(android.R.string.cancel,
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                }
                                            })
                                    .show();
                        } else {
                            doPull.run();
                        }
                    }
                }
            }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            }).show();
        }
    });
    enableDrafts = (sp.getBoolean("enableDrafts", false));
    if (sp.getBoolean("capitalizeReplies", false)) {
        etMessage.setInputType(etMessage.getInputType() | EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES);
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    tf = new ThreadFragment();
    tf.init(getLastCustomNonConfigurationInstance(), this);
    Bundle args = new Bundle();
    args.putSerializable("mid", mid);
    args.putSerializable("messagesSource", messagesSource);
    args.putSerializable("prefetched", i.getSerializableExtra("prefetched"));
    args.putSerializable("originalMessage", i.getSerializableExtra("originalMessage"));
    args.putBoolean("scrollToBottom", i.getBooleanExtra("scrollToBottom", false));
    tf.setArguments(args);
    ft.add(R.id.threadfragment, tf);
    ft.commit();
    MainActivity.restyleChildrenOrWidget(getWindow().getDecorView());
    detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (velocityX > 0 && Math.abs(velocityX) > 4 * Math.abs(velocityY) && Math.abs(velocityX) > 400) {
                if (etMessage.getText().toString().trim().length() == 0) {
                    System.out.println("velocityX=" + velocityX + " velocityY" + velocityY);
                    if (sp.getBoolean("swipeToClose", true)) {
                        onBackPressed();
                    }
                }
            }
            return false;
        }
    });

    com.actionbarsherlock.app.ActionBar actionBar = getSherlock().getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setLogo(R.drawable.back_button);

}