Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
25 kB
0
Indexable
Never
//androidmanifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.spen">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <application

        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SPen"
        tools:targetApi="31">

        <activity
            android:name=".SelectLanguageActivity_1"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".service.SpenService"
            android:enabled="true"
            android:exported="false"/>

    </application>

</manifest>

//adapter
package com.example.spen.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.spen.R;
import com.example.spen.model.LanguageName;

import java.util.List;

import javax.xml.transform.Templates;

public class SelectLanguageAdapter  extends RecyclerView.Adapter<SelectLanguageAdapter.SelectLanguageViewHolder>{

    private List<LanguageName> list;

    public SelectLanguageAdapter(List<LanguageName> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public SelectLanguageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.language_name, parent, false);
        return new SelectLanguageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SelectLanguageViewHolder holder, int position) {
        LanguageName languageName = list.get(position);
        if(languageName == null){
            return;
        }
        holder.textView.setText(languageName.getName());
    }

    @Override
    public int getItemCount() {
        if(list != null)
            return list.size();
        return 0;
    }

    public class  SelectLanguageViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;

        public SelectLanguageViewHolder(@NonNull View view) {
            super(view);
            textView = view.findViewById(R.id.lg_name);
        }
    }

}


//model
package com.example.spen.model;

public class LanguageName {
    private String name;

    public LanguageName(String name) {
        this.name = name;
    }

    public LanguageName() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


//service

package com.example.spen.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.spen.MainActivity;
import com.example.spen.R;
import com.example.spen.SelectLanguageActivity_1;

public class SpenService extends Service {


    private WindowManager mWindowManager;
    private View mFloatingView;
    private ImageView imgT;
    boolean isPlay = false;
    private TextView textViewEN, textViewVN;

    public SpenService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

        //getting the widget layout from xml using layout inflater
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);

        //setting the layout parameters
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.CENTER;        //Initially view will be added to top-left corner
        params.x = 0;
        params.y = 100;

        //getting windows services and adding the floating view to it
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);


        //The root element of the collapsed view layout
        final View collapsedView = mFloatingView.findViewById(R.id.layoutCollapsed);
        //The root element of the expanded view layout
        final View expandedView = mFloatingView.findViewById(R.id.layoutExpanded);

        //Set the close button
        ImageView buttonClose = (ImageView) mFloatingView.findViewById(R.id.buttonClose);
        buttonClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //close the service and remove the from from the window
                stopSelf();
            }
        });

        //Set the change text/sequence button
        imgT = (ImageView) mFloatingView.findViewById(R.id.btnChangeTranslateOption);
        imgT.setBackgroundResource(R.drawable.title);
        imgT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isPlay){
                    view.setBackgroundResource(R.drawable.baseline);
                }else{
                    view.setBackgroundResource(R.drawable.title);
                }

                isPlay = !isPlay;

            }
        });

        //Set the collapse button
        ImageView buttonCollapse = (ImageView) mFloatingView.findViewById(R.id.buttonCollapse);
        buttonCollapse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collapsedView.setVisibility(View.VISIBLE);
                expandedView.setVisibility(View.GONE);
            }
        });

        // set sellect language
        textViewEN= (TextView) mFloatingView.findViewById(R.id.EN);
        textViewVN= (TextView) mFloatingView.findViewById(R.id.VN);

        textViewEN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openLanguageSelect_1();
            }
        });
//        textViewVN.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                openLanguageSelect_2();
//            }
//        });

        //adding an touchlistener to make drag movement of the floating widget
        mFloatingView.findViewById(R.id.rootContainer).setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;

                    case MotionEvent.ACTION_UP:
                        int Xdiff = (int) (event.getRawX() - initialTouchX);
                        int Ydiff = (int) (event.getRawY() - initialTouchY);


                        //The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
                        //So that is click event.
                        if (Xdiff < 10 && Ydiff < 10) {
                            if (isViewCollapsed()) {
                                //When user clicks on the image view of the collapsed layout,
                                //visibility of the collapsed layout will be changed to "View.GONE"
                                //and expanded view will become visible.
                                collapsedView.setVisibility(View.GONE);
                                expandedView.setVisibility(View.VISIBLE);
                            }
                        }
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        //this code is helping the widget to move around the screen with fingers
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(mFloatingView, params);
                        return true;
                }
                return false;
            }
        });
    }
    private void openLanguageSelect_1(){
       // startService(new Intent(SpenService.this, SelectLanguageService.class));
        Intent intent = new Intent(SpenService.this, SelectLanguageActivity_1.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
//    private void openLanguageSelect_2(){
//        Intent intent = new Intent(SpenService.this, SelectLanguageActivity_1.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intent);
//    }
    private boolean isViewCollapsed() {
        return mFloatingView == null || mFloatingView.findViewById(R.id.layoutCollapsed).getVisibility() == View.VISIBLE;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
    }
}


//mainActivity
package com.example.spen;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.spen.service.SpenService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int SYSTEM_ALERT_WINDOW_PERMISSION = 2084;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            askPermission();
        }

        findViewById(R.id.buttonCreateWidget).setOnClickListener(this);
    }


    private void askPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, SYSTEM_ALERT_WINDOW_PERMISSION);
    }

    @Override
    public void onClick(View v) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            startService(new Intent(MainActivity.this, SpenService.class));
            finish();
        } else if (Settings.canDrawOverlays(this)) {
            startService(new Intent(MainActivity.this, SpenService.class));
            finish();
        } else {
            askPermission();
            Toast.makeText(this, "You need System Alert Window Permission to do this", Toast.LENGTH_SHORT).show();
        }
    }
}

//sellectactivity

package com.example.spen;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.spen.adapter.SelectLanguageAdapter;
import com.example.spen.model.LanguageName;

import java.util.ArrayList;
import java.util.List;

public class SelectLanguageActivity_1 extends AppCompatActivity {
    private RecyclerView recyclerView;
    private TextView cancel;
    private SelectLanguageAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_language_1);
        recyclerView = findViewById(R.id.lg_select);
        adapter = new SelectLanguageAdapter(getList());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        //Cancel
        cancel = findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               // findViewById(R.id.selectlanguage1).setVisibility(View.GONE);
            }
        });
    }
    private List<LanguageName> getList(){
        List<LanguageName> list = new ArrayList<>();
        list.add(new LanguageName("English"));
        list.add(new LanguageName("Japan"));
        list.add(new LanguageName("Korea"));

        list.add(new LanguageName("Chinese"));
        list.add(new LanguageName("Thailand"));
        list.add(new LanguageName("HongKong"));
        list.add(new LanguageName("English"));
        list.add(new LanguageName("Japan"));
        list.add(new LanguageName("Korea"));

        list.add(new LanguageName("Chinese"));
        list.add(new LanguageName("Thailand"));
        list.add(new LanguageName("HongKong"));
        list.add(new LanguageName("English"));
        list.add(new LanguageName("Japan"));
        list.add(new LanguageName("Korea"));

        list.add(new LanguageName("Chinese"));
        list.add(new LanguageName("Thailand"));
        list.add(new LanguageName("HongKong"));
        list.add(new LanguageName("English"));
        list.add(new LanguageName("Japan"));
        list.add(new LanguageName("Korea"));

        list.add(new LanguageName("Chinese"));
        list.add(new LanguageName("Thailand"));
        list.add(new LanguageName("HongKong1"));
        return list;
    }

}

//baseline.xml
<vector android:autoMirrored="true" android:height="24dp"
    android:tint="#F6F6F6" android:viewportHeight="35"
    android:viewportWidth="35" android:width="26dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M19,3L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM14,17L7,17v-2h7v2zM17,13L7,13v-2h10v2zM17,9L7,9L7,7h10v2z"/>
</vector>


//close.xml
<vector android:height="24dp" android:tint="#F6F6F6"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>


//languagebg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:padding="10dp">

    <solid android:color="#C4353333" />
    <corners

        android:bottomRightRadius="25dp"
        android:bottomLeftRadius="25dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp" />
</shape>

//open.xm;
<vector android:height="24dp" android:tint="#F6F6F6"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M15.41,16.59L10.83,12l4.58,-4.59L14,6l-6,6 6,6 1.41,-1.41z"/>
</vector>


//selectlangbg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:padding="10dp">
    <solid android:color="#C4353333" />

</shape>

//small.xm;
<vector android:height="24dp" android:tint="#F6F6F6"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M5.88,4.12L13.76,12l-7.88,7.88L8,22l10,-10L8,2z"/>
</vector>


//syncalt.xml

<vector android:height="24dp" android:tint="#818181"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FFFFFF" android:pathData="M22,8l-4,-4v3H3v2h15v3L22,8z"/>
    <path android:fillColor="#FFFFFF" android:pathData="M2,16l4,4v-3h15v-2H6v-3L2,16z"/>
</vector>


//title.xml

<vector android:height="24dp" android:tint="#F6F6F6"
    android:viewportHeight="35" android:viewportWidth="35"
    android:width="26dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M5,4v3h5.5v12h3V7H19V4z"/>
</vector>


//ui.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:padding="10dp">
    <stroke
        android:width="0.7dp"
        android:color="#656565" />
    <solid android:color="#272727" />
    <corners

        android:bottomRightRadius="60dp"
        android:bottomLeftRadius="60dp"
        android:topLeftRadius="60dp"
        android:topRightRadius="60dp" />
</shape>

//activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonCreateWidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Create Floating Widget" />


</RelativeLayout>

//activytiselectlanguage.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/selectlanguage1"
    android:background="@drawable/language_bg"
    tools:context=".SelectLanguageActivity_1">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="25dp"
           android:paddingBottom="45dp"
            android:layout_marginBottom="20dp"
            >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="15dp"
            android:text="Dịch từ"
            android:textColor="@color/white"
            android:textSize="20dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/lg_select"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

              />

        </LinearLayout>

    <TextView
        android:id="@+id/cancel"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:text="Hủy"
        android:textColor="@color/white"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

//languagename.xm;
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/lg_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="16dp"
        android:paddingTop="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

//layoutfoating.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!--Root container-->
    <LinearLayout
        android:id="@+id/rootContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@drawable/ui"
        android:gravity="center"
        tools:ignore="UselessParent">

        <!-- this is the expanded layout -->
        <LinearLayout
            android:id="@+id/layoutExpanded"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:visibility="visible">

            <ImageView
                android:id="@+id/buttonCollapse"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:src="@drawable/small"
                tools:ignore="ContentDescription" />

            <ImageView
                android:id="@+id/btnChangeTranslateOption"
                android:layout_width="32dp"
                android:layout_height="32dp"
               />

            <TextView
                android:id="@+id/EN"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="16dp"
                android:text="English"
                android:textColor="#FFFEFE"
                android:textSize="16dp" />

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginHorizontal="8dp"
                android:src="@drawable/sync_alt" />

            <TextView
                android:id="@+id/VN"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="16dp"
                android:text="Tiếng Việt"
                android:textColor="#FFFEFE"
                android:textSize="16dp" />

        </LinearLayout>

        <!-- this is the collapsed layout -->
        <RelativeLayout
            android:id="@+id/layoutCollapsed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone">

            <ImageView
                android:id="@+id/buttonOpen"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/open" />

        </RelativeLayout>

        <ImageView
            android:id="@+id/buttonClose"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/close" />
    </LinearLayout>
</FrameLayout>

//buildgrade(modul)

plugins {
    id 'com.android.application'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.example.spen"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}