Esempio n. 1
0
  @Override
  public void onCreate() {
    super.onCreate();
    Log.d("Launched", "Service");
    SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    final ClipboardManager clipboard =
        (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    final ClipData.Item[] item = new ClipData.Item[1];
    ClipboardManager c = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    c.addPrimaryClipChangedListener(
        new ClipboardManager.OnPrimaryClipChangedListener() {
          @Override
          public void onPrimaryClipChanged() {
            item[0] = clipboard.getPrimaryClip().getItemAt(0);
            if (item[0].getText() != null) word = item[0].getText().toString();
            Intent intent = new Intent(getApplicationContext(), ViewPagerSwipe.class);
            intent.putExtra("word", word);
            intent.addFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            getApplicationContext().startActivity(intent);
          }
        });

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.floating5);

    if (prefs.getString("ICON", "floating2").equals("floating3")) {
      chatHead.setImageResource(R.drawable.floating3);
    } else if (prefs.getString("ICON", "floating2").equals("floating4")) {
      chatHead.setImageResource(R.drawable.floating4);
    } else if (prefs.getString("ICON", "floating2").equals("floating5")) {
      chatHead.setImageResource(R.drawable.floating5);
    } else if (prefs.getString("ICON", "floating2").equals("floating5")) {
      chatHead.setImageResource(R.drawable.floating2);
    }

    final WindowManager.LayoutParams params =
        new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.START;
    params.x = 0;
    params.y = 100;
    word = "";
    //        windowManager.addView(trial,params);
    windowManager.addView(chatHead, params);

    try {
      chatHead.setOnTouchListener(
          new View.OnTouchListener() {
            public WindowManager.LayoutParams paramsF = params;
            public int initialX;
            public int initialY;
            public float initialTouchX;
            public float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
              switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                  // Get current time in nano seconds.
                  long pressTime = System.currentTimeMillis();
                  Log.d("Time", String.valueOf(pressTime));

                  // If double click...
                  if (pressTime - lastPressTime <= 300) {
                    //							createNotification();
                    Intent i = new Intent(getApplicationContext(), ServiceFloating.class);
                    ServiceFloating.this.stopService(i);
                    mHasDoubleClicked = true;
                  } else { // If not double click....
                    mHasDoubleClicked = false;
                  }
                  lastPressTime = pressTime;
                  initialX = paramsF.x;
                  initialY = paramsF.y;
                  initialTouchX = event.getRawX();
                  initialTouchY = event.getRawY();
                  break;
                case MotionEvent.ACTION_UP:
                  break;
                case MotionEvent.ACTION_MOVE:
                  paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                  paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                  windowManager.updateViewLayout(chatHead, paramsF);
                  break;
              }
              return false;
            }
          });
    } catch (Exception e) {
    }

    chatHead.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View arg0) {
            initiatePopupWindow(chatHead);
            _enable = false;
            ClipboardManager clipboard =
                (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
            word = item.getText().toString();
            Intent intent = new Intent(getApplicationContext(), ViewPagerSwipe.class);
            intent.putExtra("word", word);
            intent.addFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            getApplicationContext().startActivity(intent);
          }
        });
  }
  @Override
  public void onResume() {
    super.onResume();

    clipboardManager.addPrimaryClipChangedListener(this);
  }