Skip to content

Commit

Permalink
add process manager
Browse files Browse the repository at this point in the history
  • Loading branch information
PangBaiWork committed Sep 18, 2023
1 parent 1fc0f1d commit 6f49333
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 15 deletions.
11 changes: 10 additions & 1 deletion app/src/main/java/com/pangbai/dowork/Command/cmdExer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class cmdExer {
public static Process process = null;
public static String lastLine = null;
public static String result = null;

public static int execute(String command, boolean su) {
return execute(command, su, true);
Expand Down Expand Up @@ -40,6 +41,7 @@ public static int execute(String command, boolean su, boolean wait) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
result+=line;
lastLine = line;
}

Expand All @@ -61,6 +63,13 @@ public static int execute(String command, boolean su, boolean wait) {
}
}
}

public static void destroy(){
process=null;
result=null;
lastLine=null;
}
public static String getLastLine(){
return lastLine;
}

}
4 changes: 2 additions & 2 deletions app/src/main/java/com/pangbai/dowork/TermActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
protected void onDestroy() {
if (isChroot){
/* if (isChroot){
CommandBuilder.stopChroot();
}
}*/
TerminalSession session = mBuilder.cmdView.mTerminalSession;
if (session != null && session.isRunning())
session.finishIfRunning();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -14,28 +15,37 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.pangbai.dowork.Command.CommandBuilder;
import com.pangbai.dowork.Command.cmdExer;
import com.pangbai.dowork.R;
import com.pangbai.dowork.databinding.FragmentDashboardBinding;
import com.pangbai.dowork.preference.DoworkPreference;
import com.pangbai.dowork.service.display;
import com.pangbai.dowork.service.mainService;
import com.pangbai.dowork.service.mainServiceConnection;
import com.pangbai.dowork.service.serviceCallback;
import com.pangbai.dowork.tool.IO;
import com.pangbai.dowork.tool.Init;
import com.pangbai.dowork.tool.containerInfor;
import com.pangbai.dowork.tool.procAdapter;
import com.pangbai.dowork.tool.procMap;
import com.pangbai.dowork.tool.uiThreadUtil;
import com.pangbai.dowork.tool.util;
import com.pangbai.dowork.*;
import com.pangbai.linuxdeploy.PrefStore;
import com.pangbai.view.dialogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//import leakcanary.LeakCanary;

public class dashboardFragment extends Fragment implements View.OnClickListener {
public class dashboardFragment extends Fragment implements View.OnClickListener, serviceCallback {
FragmentDashboardBinding binding;
//containerInfor currentContainer;
mainServiceConnection serviceConnection;
Expand All @@ -49,6 +59,7 @@ public class dashboardFragment extends Fragment implements View.OnClickListener
public void onAttach(@NonNull Context context) {
super.onAttach(context);
binding = FragmentDashboardBinding.inflate(getLayoutInflater());
binding.listProc.setLayoutManager(new LinearLayoutManager(getContext()));

}

Expand All @@ -62,6 +73,7 @@ public View onCreateView(
// binding = FragmentDashboardBinding.inflate(getLayoutInflater());
binding.ctTerminal.setOnClickListener(this);
binding.ctStartStop.setOnClickListener(this);
binding.umount.setOnClickListener(this);

executorService = Executors.newSingleThreadExecutor();
if (mainService.isCmdRunning) {
Expand Down Expand Up @@ -98,6 +110,7 @@ public void run() {
size = IO.countDirSize(path);
else
size = null;
cmdExer.destroy();

Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
Expand Down Expand Up @@ -138,8 +151,8 @@ public void run() {
}
try {
//wait For service Start;
// if (display.mService == null)
Thread.sleep(500);
// if (display.mService == null)
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -151,12 +164,29 @@ public void run() {
binding.displayStatus.setText(status);
}
});
if (!Init.isRoot)
return;
procAdapter adapter = getChrootProcAdapter();
uiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
if (adapter != null) {
// binding.processCount.setText("Process: "+adapter.getItemCount());
binding.listProc.setAdapter(adapter);
callback(adapter.getItemCount());
} else {
callback(0);
}
// adapter.notifyDataSetChanged();
}
});


}
});
}


public String checkXStatus() {
boolean x11, xserver, pulseaudio;
x11 = display.Xdraw != null && display.Xdraw.isAlive();
Expand All @@ -181,7 +211,24 @@ public String checkPermission() {
return "Root:" + (root ? allow : denied) + "\nWindow:" + (window ? allow : denied);
}


public procAdapter getChrootProcAdapter() {
cmdExer.execute("ls -l /proc/*/root | grep /data/local", true, true);

if (cmdExer.result == null) return null;
Log.e("proc", cmdExer.result);
List<String> pids = util.getByTwoString(cmdExer.result, "proc/", "/root");
List<procMap> mprocMap = new ArrayList<>();
cmdExer.destroy();
for (String pid : pids) {
int com = cmdExer.execute("cat " + "/proc/" + pid + "/comm", true, true);
if (com != -1 && cmdExer.lastLine != null)
mprocMap.add(new procMap(pid, cmdExer.lastLine));
}
if (mprocMap.size() != 0)
return new procAdapter(mprocMap, this);
else
return null;
}


@Override
Expand Down Expand Up @@ -216,9 +263,9 @@ public void onClick(View view) {
serviceConnection = null;
binding.ctStartStop.setBackgroundResource(R.drawable.ct_start);
} else {
// Toast.makeText(getContext(), "s", Toast.LENGTH_LONG).show();
if (!containerInfor.checkInstall(getContext())){
Toast.makeText(getContext(),"容器未安装",Toast.LENGTH_LONG).show();
// Toast.makeText(getContext(), "s", Toast.LENGTH_LONG).show();
if (!containerInfor.checkInstall(getContext())) {
Toast.makeText(getContext(), "容器未安装", Toast.LENGTH_LONG).show();
return;
}
dialogUtils.showInputDialog(getContext(),
Expand All @@ -239,6 +286,15 @@ public void onClick(View view) {
getActivity().bindService(mIntent, serviceConnection, Context.BIND_AUTO_CREATE);
});
}
} else if (view==binding.umount) {
if (containerInfor.isProot(containerInfor.ct)){
Toast.makeText(getContext(),"Only for chroot",Toast.LENGTH_SHORT).show();
return;
}
CommandBuilder.stopChroot();
Toast.makeText(getContext(),"umounted",Toast.LENGTH_SHORT).show();
doInBackground(containerInfor.ct.path);

}
}

Expand All @@ -250,7 +306,7 @@ public void onResume() {
String name = PrefStore.getProfileName(getContext());
containerInfor.ct = containerInfor.getContainerInfor(name);
if (containerInfor.ct == null) {
// Toast.makeText(getContext(), "no container found", Toast.LENGTH_LONG).show();
// Toast.makeText(getContext(), "no container found", Toast.LENGTH_LONG).show();
return;

}
Expand All @@ -264,4 +320,9 @@ public void onResume() {


}

@Override
public void callback(int result) {
binding.processCount.setText("Process: " + result);
}
}
71 changes: 71 additions & 0 deletions app/src/main/java/com/pangbai/dowork/tool/procAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.pangbai.dowork.tool;

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

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

import com.pangbai.dowork.Command.cmdExer;
import com.pangbai.dowork.R;
import com.pangbai.dowork.service.serviceCallback;

import java.util.List;

public class procAdapter extends RecyclerView.Adapter<procAdapter.ViewHolder> {
private List<procMap> map;
serviceCallback callback;

public procAdapter(List<procMap> itemList, serviceCallback callback) {
this.map = itemList;
this.callback=callback;
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String text = map.get(position).name;
holder.procName.setText(text);

holder.procKill.setOnClickListener(v -> {
Toast.makeText(v.getContext(),"已结束进程"+ map.get(position).pid,Toast.LENGTH_SHORT).show();
cmdExer.execute("kill -9 "+map.get(position).pid,true,false);
map.remove(position);
callback.callback(map.size());
notifyDataSetChanged();

//todo
});
}

@Override
public int getItemCount() {
return map.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView procName;
TextView procKill;

public ViewHolder(@NonNull View itemView) {
super(itemView);
procName = itemView.findViewById(R.id.proc_name);
procKill = itemView.findViewById(R.id.proc_kill);
}
}


}


9 changes: 9 additions & 0 deletions app/src/main/java/com/pangbai/dowork/tool/procMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.pangbai.dowork.tool;

public class procMap {
String pid,name;
public procMap(String pid,String name){
this.pid=pid;
this.name=name;
}
}
17 changes: 16 additions & 1 deletion app/src/main/java/com/pangbai/dowork/tool/util.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,25 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class util {


public static List<String> getByTwoString(String input, String one, String two){
Pattern pattern = Pattern.compile(one+"(.*?)"+two);
Matcher matcher = pattern.matcher(input);
List<String> result=new ArrayList<>();

// 查找匹配项并输出内容
while (matcher.find()) {
result.add(matcher.group(1)); // 获取第一个捕获组的内容
// System.out.println("提取的内容: " + extracted);
}
return result;
}
public static void startActivity(Context ct, Class activity, boolean anim) {
Intent it = new Intent(ct.getApplicationContext(), activity);
if (anim) {
Expand Down
26 changes: 23 additions & 3 deletions app/src/main/res/layout/fragment_dashboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,31 @@

</LinearLayout>

<ListView
android:id="@+id/ct_list"
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="10dp"
android:id="@+id/list_proc"
android:layout_width="match_parent"
android:layout_height="0"
android:layout_height="wrap_content"
android:layout_below="@id/ct_current" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/process_count"
android:text="Process: ..."
android:layout_marginLeft="5dp"
android:textColor="@color/white"
style="@style/defaultTextStyle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/umount"
android:background="@drawable/view_circle"
android:layout_marginTop="5dp"
android:text="umount container"
android:padding="6dp"
android:textColor="@color/black"
style="@style/defaultTextStyle"/>


<com.pangbai.view.CustomTextView
android:layout_marginTop="20dp"
Expand Down
Loading

0 comments on commit 6f49333

Please sign in to comment.