0

i'm writing service class that has image overlay imageView overlay was visible, but it isn't visible after i added imageview resize code. Also, MotionEvent for resized imageview isn't works well Why imageview overlay isn't visible and MotionEvent isn't works well? Could you help me to solve this problem?

here's my code

This code show ImageView overlay which isn't resized, it is visible and MotionEvent works well

package kr.hybdms.sidepanel;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.graphics.PixelFormat;

public class TouchDetectService extends Service {
    private ImageView mTouchDetector;                           
    private WindowManager.LayoutParams mParams;     
    private WindowManager mWindowManager;   

    private static final int MY_NOTIFICATION_ID=1;
    private NotificationManager notificationManager;
    private Notification myNotification;
    final static String ACTION = "NotifyServiceAction";
    final static String STOP_SERVICE = "";
    final static int RQS_STOP_SERVICE = 1;


    private OnTouchListener mViewTouchListener = new OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {

                case MotionEvent.ACTION_MOVE:
                    Intent lsp = new Intent(getBaseContext(), SidePanel.class);
                    lsp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplication().startActivity(lsp);
                    break;
            }
            return true;
        }
    };
    @Override
    public IBinder onBind(Intent arg0) { return null; }
    @Override
    public void onCreate() {
        super.onCreate();     
        boolean rightpanel = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("panelpos_right", true);
        boolean notificationison = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("noti_toggle", true);
        Log.i("BOOTSVC", "Service started at the BOOT_COMPLETED.");
        if(rightpanel)
        {
        mTouchDetector = new ImageView(this);
        mTouchDetector.setImageResource(R.drawable.detector);
        mTouchDetector.setOnTouchListener(mViewTouchListener); 
        mParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,  
            PixelFormat.TRANSLUCENT);                             
        mParams.gravity = Gravity.RIGHT | Gravity.CENTER;             
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
        mWindowManager.addView(mTouchDetector, mParams);    
        }
        else
        {
            mTouchDetector = new ImageView(this);                                         
            mTouchDetector.setImageResource(R.drawable.detector);
            mTouchDetector.setOnTouchListener(mViewTouchListener);            
            mParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,  
                PixelFormat.TRANSLUCENT);                                      
            mParams.gravity = Gravity.LEFT | Gravity.CENTER;        
            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);  
            mWindowManager.addView(mTouchDetector, mParams);      
        }

        if(notificationison){
        notificationManager =
         (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        myNotification = new Notification(R.drawable.ic_stat_sidepanel,
                getText(R.string.service_notification),
          System.currentTimeMillis());
        Context context = getApplicationContext();
        CharSequence notificationTitle = getText(R.string.service_running);
        CharSequence notificationText = getText(R.string.service_running_desc);
        Intent myIntent = new Intent(getBaseContext(), Settings.class);;
        PendingIntent pendingIntent
          = PendingIntent.getActivity(getBaseContext(),
            0, myIntent,
            Intent.FLAG_ACTIVITY_NEW_TASK);
        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        myNotification.flags = Notification.FLAG_ONGOING_EVENT;
        myNotification.setLatestEventInfo(context,
           notificationTitle,
           notificationText,
           pendingIntent);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
        }
        else
        {
        }
        }
    @Override
    public void onDestroy() {
        if(mWindowManager != null) {      
            if(mTouchDetector != null) mWindowManager.removeView(mTouchDetector);
        }
        super.onDestroy();
        notificationManager.cancel(MY_NOTIFICATION_ID);
    }
}

This code show ImageView overlay which resized, it isn't visible and MotionEvent isn't works well imageview is resizes by preference value

package kr.hybdms.sidepanel;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.graphics.PixelFormat;

public class TouchDetectService extends Service {
    private ImageView mTouchDetector;                           
    private WindowManager.LayoutParams mParams;     
    private WindowManager mWindowManager;   
    private static final int MY_NOTIFICATION_ID=1;
    private NotificationManager notificationManager;
    private Notification myNotification;
    final static String ACTION = "NotifyServiceAction";
    final static String STOP_SERVICE = "";
    final static int RQS_STOP_SERVICE = 1;


    private OnTouchListener mViewTouchListener = new OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    boolean vibeon = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("vibe_toggle", true);
                    if(vibeon){ 
                    Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    vibe.vibrate(10);} 
                    else{}
                    Intent lsp = new Intent(getBaseContext(), SidePanel.class);
                    lsp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplication().startActivity(lsp);
                    break;
            }
            return true;
        }
    };

    @Override
    public IBinder onBind(Intent arg0) { return null; }
    @Override
    public void onCreate() {
        super.onCreate();
        boolean rightpanel = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("panelpos_right", true);
        boolean notificationison = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("noti_toggle", true);
        Log.i("BOOTSVC", "Service started at the BOOT_COMPLETED.");
            SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
          String dw = myPreference.getString("detector_width", "");
          String dh = myPreference.getString("detector_height", "");
        mTouchDetector = new ImageView(this);                                       
        mTouchDetector.setImageResource(R.drawable.detector);
        mTouchDetector.setOnTouchListener(mViewTouchListener);              
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();

        if(dw.equals("025")){
            params.height = (int) (mTouchDetector.getDrawable().getIntrinsicWidth()*0.25);
        }
        else if(dw.equals("050")){
            params.height = (int) (mTouchDetector.getDrawable().getIntrinsicWidth()*0.5);
        }
        else if(dw.equals("075")){
            params.height = (int) (mTouchDetector.getDrawable().getIntrinsicWidth()*0.75);
        }
        else if(dw.equals("100")){
            params.height = mTouchDetector.getDrawable().getIntrinsicWidth();
        }
        else if(dw.equals("200")){
            params.height = mTouchDetector.getDrawable().getIntrinsicWidth()*2;
        }
        else if(dw.equals("300")){
            params.height = mTouchDetector.getDrawable().getIntrinsicWidth()*3;
        }
        else if(dw.equals("400")){
            params.height = mTouchDetector.getDrawable().getIntrinsicWidth()*4;
        }

        if (dh.equals("025")){
            params.width = (int) (mTouchDetector.getDrawable().getIntrinsicHeight()*0.25);
            }
        else if (dh.equals("050")){
            params.width = (int) (mTouchDetector.getDrawable().getIntrinsicHeight()*0.5);
            }
        else if (dh.equals("075")){
            params.width = (int) (mTouchDetector.getDrawable().getIntrinsicHeight()*0.75);
            }
        else if (dh.equals("100")){
            params.width = mTouchDetector.getDrawable().getIntrinsicHeight();
            }
        else if (dh.equals("200")){
            params.width = mTouchDetector.getDrawable().getIntrinsicHeight()*2;
            }
        else if (dh.equals("300")){
            params.width = mTouchDetector.getDrawable().getIntrinsicHeight()*3;
            }
        else if (dh.equals("400")){
            params.width = mTouchDetector.getDrawable().getIntrinsicHeight()*4;
            }
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;

        if(rightpanel)
        {
        params.gravity = Gravity.RIGHT & Gravity.CENTER;   
        }
        else
        {
            params.gravity = Gravity.LEFT & Gravity.CENTER;   
        }
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mTouchDetector, params);
        if(notificationison){
        notificationManager =
         (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        myNotification = new Notification(R.drawable.ic_stat_sidepanel,
                getText(R.string.service_notification),
          System.currentTimeMillis());
        Context context = getApplicationContext();
        CharSequence notificationTitle = getText(R.string.service_running);
        CharSequence notificationText = getText(R.string.service_running_desc);
        Intent myIntent = new Intent(getBaseContext(), Settings.class);;
        PendingIntent pendingIntent
          = PendingIntent.getActivity(getBaseContext(),
            0, myIntent,
            Intent.FLAG_ACTIVITY_NEW_TASK);
        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        myNotification.flags = Notification.FLAG_ONGOING_EVENT;
        myNotification.setLatestEventInfo(context,
           notificationTitle,
           notificationText,
           pendingIntent);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
        }
        else
        {
        }
        }
    @Override
    public void onDestroy() {
        if(mWindowManager != null) {       
            if(mTouchDetector != null) mWindowManager.removeView(mTouchDetector);
        }
        super.onDestroy();
        notificationManager.cancel(MY_NOTIFICATION_ID);
    }
}
sukso
  • 145
  • 1
  • 8

1 Answers1

0

The code that you are complaining about isn't adding the ImageView - mTouchDetector - to your layout. You must add it in order for it to be visible if you are using plain Java instead of XML. Look at your first example (the working one):

mWindowManager.addView(mTouchDetector, mParams); 

That's the idea here.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147