Skip to content

Commit

Permalink
update demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzuchang committed Mar 8, 2023
1 parent 07f61a6 commit ef191b6
Show file tree
Hide file tree
Showing 72 changed files with 1,937 additions and 812 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
android:name="android.app.lib_name"
android:value="" />
</activity>

<activity android:name="cn.tongdun.android.activity.ItemListActivity" />
</application>

</manifest>
201 changes: 201 additions & 0 deletions app/src/main/java/cn/tongdun/android/activity/ItemListActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package cn.tongdun.android.activity;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.trustdevice.android.R;

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

import butterknife.BindView;
import butterknife.OnClick;
import cn.tongdun.android.adapter.AppListRecyclerViewAdapter;
import cn.tongdun.android.base.BaseActivity;
import cn.tongdun.android.beans.AppItemData;

public class ItemListActivity extends BaseActivity {

@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.tv_title_right)
TextView tvTitleRight;
@BindView(R.id.rv_item_list)
RecyclerView rvItemList;

private int mType;
private boolean mShowSystemApp = false;
private List<AppItemData> mItemData = new ArrayList<>();
private AppListRecyclerViewAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
protected int getContentViewResId() {
return R.layout.activity_app_list;
}

@Override
protected void initData() {
mType = getIntent().getIntExtra("type", -1);
if (mType == 0) {
loadInstalledAppList(mShowSystemApp);
} else if (mType == 1) {
loadSensorList();
}
}

@Override
protected void initView() {
if (mType == 0) {
tvTitle.setText(getResources().getString(R.string.app_list));
tvTitleRight.setText(getResources().getString(R.string.show_system_apps));
} else if (mType == 1) {
tvTitle.setText(getResources().getString(R.string.sensor_list));
tvTitleRight.setVisibility(View.GONE);
}
LinearLayoutManager manager = new LinearLayoutManager(this);
rvItemList.setLayoutManager(manager);
mAdapter = new AppListRecyclerViewAdapter(mItemData);
rvItemList.setAdapter(mAdapter);
rvItemList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
}

@OnClick({R.id.tv_title, R.id.tv_title_right})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.tv_title:
this.finish();
break;
case R.id.tv_title_right:
mShowSystemApp = !mShowSystemApp;
if (mShowSystemApp) {
tvTitleRight.setText(getResources().getString(R.string.hide_system_apps));
} else {
tvTitleRight.setText(getResources().getString(R.string.show_system_apps));
}
loadInstalledAppList(mShowSystemApp);
mAdapter.updateData(mItemData);
break;
}
}

private void loadInstalledAppList(boolean showSystemApp) {
PackageManager packageManager = getPackageManager();
if (packageManager == null) {
return;
}
List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
for (PackageInfo info : packageInfoList) {
if (info == null) {
continue;
}
if (!showSystemApp && isSystemApp(info.applicationInfo)) {
continue;
}
String packageName = info.packageName;
String versionName = info.versionName;
Drawable icon = info.applicationInfo.loadIcon(packageManager);
String appName = packageManager.getApplicationLabel(info.applicationInfo).toString();
// if (mAppItemDataSet.contains(appName)){
// continue;
// }
mItemData.add(new AppItemData(icon, appName, packageName, versionName));
}
}

private boolean isSystemApp(ApplicationInfo info) {
boolean isSysApp = (info.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM;
boolean isSysUpd = (info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
return isSysApp || isSysUpd;
}

private void loadSensorList() {

SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensorList) {
Drawable icon = ContextCompat.getDrawable(this, getIconByType(sensor.getType()));
String name = sensor.getName();
String vendor = sensor.getVendor();
String version = String.valueOf(sensor.getVersion());
mItemData.add(new AppItemData(icon, name, vendor, version));
}
}

private int getIconByType(int sensorType) {
switch (sensorType) {
case 1:
case 10:
case 35:
return R.drawable.ic_sensor_accelerometer;
case 2:
case 14:
return R.drawable.ic_sensor_magnetic_field;
case 3:
case 11:
case 15:
case 20:
case 27:
return R.drawable.ic_sensor_orientation;
case 4:
case 16:
return R.drawable.ic_sensor_gyroscope;
case 5:
return R.drawable.ic_sensor_light;
case 6:
return R.drawable.ic_sensor_pressure;
case 7:
case 13:
return R.drawable.ic_sensor_temperature;
case 8:
return R.drawable.ic_sensor_proximity;
case 9:
return R.drawable.ic_sensor_gravity;
case 12:
return R.drawable.ic_sensor_humidity;
case 17:
case 30:
return R.drawable.ic_sensor_motion;
case 18:
case 19:
return R.drawable.ic_sensor_step;
case 21:
case 31:
return R.drawable.ic_sensor_heartrate;
case 22:
case 26:
return R.drawable.ic_sensor_tilt;
case 23:
case 24:
case 25:
case 28:
case 29:
case 32:
case 33:
case 34:
return R.drawable.ic_sensor_all;
case 36:
return R.drawable.ic_sensor_hinge;
default:
return R.drawable.ic_sensor_private;
}
}

}
98 changes: 28 additions & 70 deletions app/src/main/java/cn/tongdun/android/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
package cn.tongdun.android.activity;

import android.text.TextUtils;
import android.util.Pair;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.trustdevice.android.R;

import org.json.JSONObject;

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

import butterknife.BindView;
import cn.tongdun.android.adapter.DeviceModuleItemAdapter;
import cn.tongdun.android.adapter.ViewPagerAdapter;
import cn.tongdun.android.base.BaseActivity;
import cn.tongdun.android.beans.DetailsItemBean;
import cn.tongdun.android.beans.DeviceModuleItemBean;
import cn.tongdun.mobrisk.TDRisk;
import cn.tongdun.mobrisk.TDRiskCallback;
import cn.tongdun.mobrisk.providers.InfoProvider;
import cn.tongdun.mobrisk.providers.applist.AppListProvider;
import cn.tongdun.mobrisk.providers.battery.BatteryInfoProvider;
import cn.tongdun.mobrisk.providers.build.BuildInfoProvider;
import cn.tongdun.mobrisk.providers.cpu.CpuInfoProvider;
import cn.tongdun.mobrisk.providers.device_id.DeviceIdProvider;
import cn.tongdun.mobrisk.providers.memory.MemoryInfoProvider;
import cn.tongdun.mobrisk.providers.risk.RiskInfoProvider;
import cn.tongdun.mobrisk.providers.screen.ScreenInfoProvider;
import cn.tongdun.mobrisk.providers.sensor.SensorInfoProvider;
import cn.tongdun.mobrisk.providers.setting.SettingInfoProvider;

public class MainActivity extends BaseActivity {

@BindView(R.id.device_info_container)
RecyclerView rvDeviceInfoContainer;
private DeviceModuleItemAdapter mAdapter;
private List<DeviceModuleItemBean> mItemData = new ArrayList<>();
@BindView(R.id.tab_layout)
TabLayout mTabLayout;
@BindView(R.id.viewpager)
ViewPager2 mViewPager;

private final List<String> mTabList = new ArrayList<>();
private TabLayoutMediator mTabLayoutMediator;

@Override
protected int getContentViewResId() {
Expand All @@ -47,54 +32,27 @@ protected int getContentViewResId() {

@Override
protected void initData() {
createDeviceInfo();
mTabList.add("Device id");
mTabList.add("Device info");
}

@Override
protected void initView() {
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(GridLayoutManager.VERTICAL);
rvDeviceInfoContainer.setLayoutManager(manager);
mAdapter = new DeviceModuleItemAdapter(mItemData);
rvDeviceInfoContainer.setAdapter(mAdapter);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(mTabList.size(), getSupportFragmentManager(), getLifecycle());
mViewPager.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT);
mViewPager.setAdapter(viewPagerAdapter);
mTabLayoutMediator = new TabLayoutMediator(mTabLayout, mViewPager, (tab, position) -> {
TextView tabView = new TextView(MainActivity.this);
tabView.setText(mTabList.get(position));
tab.setCustomView(tabView);
});
// binding
mTabLayoutMediator.attach();
}

private void createDeviceInfo() {
JSONObject deviceInfo = TDRisk.getBlackbox();
createData(deviceInfo);
}

private void createData(JSONObject deviceInfo) {
createItemInfo(new DeviceIdProvider(deviceInfo));
createItemInfo(new RiskInfoProvider(deviceInfo));
createItemInfo(new BuildInfoProvider(deviceInfo));
createItemInfo(new ScreenInfoProvider(deviceInfo));
createItemInfo(new BatteryInfoProvider(deviceInfo));
createItemInfo(new SettingInfoProvider(deviceInfo));
createItemInfo(new CpuInfoProvider(deviceInfo));
createItemInfo(new MemoryInfoProvider(deviceInfo));
createItemInfo(new SensorInfoProvider(deviceInfo));
createAppList(new AppListProvider(deviceInfo));
}

private void createItemInfo(InfoProvider<String> infoProvider) {
List<DetailsItemBean> detailsItemBeanList = new ArrayList<>();
for (Pair<String, String> infos : infoProvider.getRawData()) {
detailsItemBeanList.add(new DetailsItemBean(infos.first, infos.second));
}
mItemData.add(new DeviceModuleItemBean(infoProvider.getProviderName(), detailsItemBeanList));
}

private void createAppList(InfoProvider<String[]> appListProvider) {
for (Pair<String, String[]> infos : appListProvider.getRawData()) {
List<DetailsItemBean> appListDetailsItemList = new ArrayList<>();
for (String appInfo : infos.second) {
if (TextUtils.isEmpty(appInfo)) {
continue;
}
appListDetailsItemList.add(new DetailsItemBean(appInfo, ""));
}
mItemData.add(new DeviceModuleItemBean(infos.first, appListDetailsItemList));
}
@Override
protected void onDestroy() {
mTabLayoutMediator.detach();
super.onDestroy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cn.tongdun.android.adapter;

import android.widget.ImageView;
import android.widget.TextView;

import com.trustdevice.android.R;

import java.util.List;

import cn.tongdun.android.beans.AppItemData;

/**
* @description: app list RecyclerView Adapter
* @author: wuzuchang
* @date: 2023/3/8
*/
public class AppListRecyclerViewAdapter extends BaseRecyclerViewAdapter<AppItemData> {

public AppListRecyclerViewAdapter(List<AppItemData> data) {
super(data);
}

@Override
public int getLayoutId(int viewType) {

return R.layout.item_app_list;
}

@Override
public void convert(BaseViewHolder holder, final AppItemData data, final int position) {

ImageView iv_icon = holder.getView(R.id.iv_app_icon);
iv_icon.setImageDrawable(data.getIcon());
TextView tv_app_name = holder.getView(R.id.tv_app_name);
tv_app_name.setText(data.getAppName() + ": " + data.getVersionName());
TextView tv_application = holder.getView(R.id.tv_application);
tv_application.setText(data.getPackageName());
}


public void updateData(List<AppItemData> data) {
this.mData = data;
notifyDataSetChanged();
}
}
Loading

0 comments on commit ef191b6

Please sign in to comment.