|
| 1 | +package com.balaganovrocks.yourmastercleaner.adapter; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Handler; |
| 5 | +import android.view.LayoutInflater; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.BaseAdapter; |
| 9 | +import android.widget.ImageView; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import com.balaganovrocks.yourmastercleaner.fragment.AutoStartFragment; |
| 13 | +import com.balaganovrocks.yourmastercleaner.R; |
| 14 | +import com.balaganovrocks.yourmastercleaner.model.AutoStartInfo; |
| 15 | +import com.balaganovrocks.yourmastercleaner.utils.ShellUtils; |
| 16 | +import com.balaganovrocks.yourmastercleaner.utils.T; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +public class AutoStartAdapter extends BaseAdapter { |
| 22 | + |
| 23 | + public List<AutoStartInfo> mlistAppInfo; |
| 24 | + LayoutInflater infater = null; |
| 25 | + private Context mContext; |
| 26 | + public static List<Integer> clearIds; |
| 27 | + private Handler mHandler; |
| 28 | + |
| 29 | + public AutoStartAdapter(Context context, List<AutoStartInfo> apps, Handler mHandler) { |
| 30 | + infater = LayoutInflater.from(context); |
| 31 | + mContext = context; |
| 32 | + clearIds = new ArrayList<Integer>(); |
| 33 | + this.mlistAppInfo = apps; |
| 34 | + this.mHandler = mHandler; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public int getCount() { |
| 39 | + // TODO Auto-generated method stub |
| 40 | + return mlistAppInfo.size(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Object getItem(int position) { |
| 45 | + // TODO Auto-generated method stub |
| 46 | + return mlistAppInfo.get(position); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public long getItemId(int position) { |
| 51 | + // TODO Auto-generated method stub |
| 52 | + return position; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public View getView(int position, View convertView, ViewGroup parent) { |
| 57 | + ViewHolder holder = null; |
| 58 | + if (convertView == null) { |
| 59 | + convertView = infater.inflate(R.layout.listview_auto_start, |
| 60 | + null); |
| 61 | + holder = new ViewHolder(); |
| 62 | + holder.appIcon = (ImageView) convertView |
| 63 | + .findViewById(R.id.app_icon); |
| 64 | + holder.appName = (TextView) convertView |
| 65 | + .findViewById(R.id.app_name); |
| 66 | + holder.size = (TextView) convertView |
| 67 | + .findViewById(R.id.app_size); |
| 68 | + holder.disable_switch = (TextView) convertView |
| 69 | + .findViewById(R.id.disable_switch); |
| 70 | + |
| 71 | + convertView.setTag(holder); |
| 72 | + } else { |
| 73 | + holder = (ViewHolder) convertView.getTag(); |
| 74 | + } |
| 75 | + final AutoStartInfo item = (AutoStartInfo) getItem(position); |
| 76 | + if (item != null) { |
| 77 | + holder.appIcon.setImageDrawable(item.getIcon()); |
| 78 | + holder.appName.setText(item.getLabel()); |
| 79 | + if (item.isEnable()) { |
| 80 | + holder.disable_switch.setBackgroundResource(R.drawable.switch_on); |
| 81 | + holder.disable_switch.setText("已开启"); |
| 82 | + } else { |
| 83 | + holder.disable_switch.setBackgroundResource(R.drawable.switch_off); |
| 84 | + holder.disable_switch.setText("已禁止"); |
| 85 | + } |
| 86 | + // holder.size.setText(Formatter.formatShortFileSize(mContext, item.getCacheSize())); |
| 87 | + |
| 88 | + holder.disable_switch.setOnClickListener(new View.OnClickListener() { |
| 89 | + @Override |
| 90 | + public void onClick(View v) { |
| 91 | + |
| 92 | + |
| 93 | + if (ShellUtils.checkRootPermission()) { |
| 94 | + |
| 95 | + if (item.isEnable()) { |
| 96 | + |
| 97 | + |
| 98 | + diasableApp(item); |
| 99 | + } else { |
| 100 | + |
| 101 | + enableApp(item); |
| 102 | + } |
| 103 | + |
| 104 | + } else { |
| 105 | + |
| 106 | + T.showLong(mContext, "该功能需要获取系统root权限,点击允许获取root权限"); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + }); |
| 112 | + holder.packageName = item.getPackageName(); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + return convertView; |
| 117 | + } |
| 118 | + |
| 119 | + private void diasableApp(AutoStartInfo item) { |
| 120 | + String packageReceiverList[] = item.getPackageReceiver().toString().split(";"); |
| 121 | + |
| 122 | + List<String> mSring = new ArrayList<>(); |
| 123 | + for (int j = 0; j < packageReceiverList.length; j++) { |
| 124 | + String cmd = "pm disable " + packageReceiverList[j]; |
| 125 | + //部分receiver包含$符号,需要做进一步处理,用"$"替换掉$ |
| 126 | + cmd = cmd.replace("$", "\"" + "$" + "\""); |
| 127 | + //执行命令 |
| 128 | + mSring.add(cmd); |
| 129 | + |
| 130 | + } |
| 131 | + ShellUtils.CommandResult mCommandResult = ShellUtils.execCommand(mSring, true, true); |
| 132 | + |
| 133 | + if (mCommandResult.result == 0) { |
| 134 | + T.showLong(mContext, item.getLabel() + "已禁止"); |
| 135 | + item.setEnable(false); |
| 136 | + notifyDataSetChanged(); |
| 137 | + if (mHandler != null) { |
| 138 | + mHandler.sendEmptyMessage(AutoStartFragment.REFRESH_BT); |
| 139 | + } |
| 140 | + } else { |
| 141 | + T.showLong(mContext, item.getLabel() + "禁止失败"); |
| 142 | + } |
| 143 | + |
| 144 | + // T.showLong(mContext, mCommandResult.result + "" + mCommandResult.errorMsg + mCommandResult.successMsg); |
| 145 | + } |
| 146 | + |
| 147 | + private void enableApp(AutoStartInfo item) { |
| 148 | + String packageReceiverList[] = item.getPackageReceiver().toString().split(";"); |
| 149 | + |
| 150 | + List<String> mSring = new ArrayList<>(); |
| 151 | + for (int j = 0; j < packageReceiverList.length; j++) { |
| 152 | + String cmd = "pm enable " + packageReceiverList[j]; |
| 153 | + //部分receiver包含$符号,需要做进一步处理,用"$"替换掉$ |
| 154 | + cmd = cmd.replace("$", "\"" + "$" + "\""); |
| 155 | + //执行命令 |
| 156 | + mSring.add(cmd); |
| 157 | + |
| 158 | + } |
| 159 | + ShellUtils.CommandResult mCommandResult = ShellUtils.execCommand(mSring, true, true); |
| 160 | + |
| 161 | + if (mCommandResult.result == 0) { |
| 162 | + T.showLong(mContext, item.getLabel() + "已开启"); |
| 163 | + item.setEnable(true); |
| 164 | + notifyDataSetChanged(); |
| 165 | + if (mHandler != null) { |
| 166 | + mHandler.sendEmptyMessage(AutoStartFragment.REFRESH_BT); |
| 167 | + } |
| 168 | + } else { |
| 169 | + T.showLong(mContext, item.getLabel() + "开启失败"); |
| 170 | + } |
| 171 | + // T.showLong(mContext, mCommandResult.result + "" + mCommandResult.errorMsg + mCommandResult.successMsg); |
| 172 | + } |
| 173 | + |
| 174 | + class ViewHolder { |
| 175 | + ImageView appIcon; |
| 176 | + TextView appName; |
| 177 | + TextView size; |
| 178 | + TextView disable_switch; |
| 179 | + |
| 180 | + String packageName; |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments