addrs = Collections.list(intf.getInetAddresses());
60 | for (InetAddress addr : addrs) {
61 | if (!addr.isLoopbackAddress()) {
62 | String sAddr = addr.getHostAddress().toUpperCase();
63 | boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
64 | if (useIPv4) {
65 | if (isIPv4)
66 | return sAddr;
67 | } else {
68 | if (!isIPv4) {
69 | int delim = sAddr.indexOf('%'); // drop ip6 port suffix
70 | return delim < 0 ? sAddr : sAddr.substring(0, delim);
71 | }
72 | }
73 | }
74 | }
75 | }
76 | } catch (final Exception ignored) {
77 | // for now eat exceptions
78 | }
79 | return "";
80 | }
81 |
82 | public static String getUserAgent() {
83 | return new WebView(Device.getContext()).getSettings().getUserAgentString();
84 | }
85 |
86 | public static ProxySettings getProxySettings() {
87 | return new ProxySettings(Device.getContext());
88 | }
89 |
90 | /**
91 | * Returns MAC Address.
92 | *
93 | * IMPORTANT! requires {@link android.Manifest.permission#ACCESS_WIFI_STATE}
94 | *
95 | * Disadvantages:
96 | * - Device should have Wi-Fi (where not all devices have Wi-Fi)
97 | * - If Wi-Fi present in Device should be turned on otherwise does not report the MAC address
98 | */
99 | public static String getMacAdress() {
100 | return getWifiManager().getConnectionInfo().getMacAddress();
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/app/src/main/java/net/kibotu/android/deviceinfo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.content.res.Configuration;
6 | import android.os.Bundle;
7 | import android.view.WindowManager;
8 | import android.widget.TextView;
9 | import androidx.annotation.CallSuper;
10 | import androidx.annotation.NonNull;
11 | import androidx.appcompat.app.AppCompatActivity;
12 | import androidx.fragment.app.Fragment;
13 | import com.exozet.android.core.utils.DeviceExtensions;
14 | import com.exozet.android.core.utils.FragmentExtensions;
15 | import com.robohorse.gpversionchecker.GPVersionChecker;
16 | import com.robohorse.gpversionchecker.base.CheckingStrategy;
17 | import net.kibotu.android.deviceinfo.ui.BaseFragment;
18 | import net.kibotu.android.deviceinfo.ui.buildinfo.BuildFragment;
19 | import net.kibotu.android.deviceinfo.ui.menu.Menu;
20 | import net.kibotu.logger.Logger;
21 | import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
22 |
23 | import static com.exozet.android.core.extensions.FragmentExtensions.currentFragment;
24 | import static com.exozet.android.core.extensions.ResourceExtensions.getResColor;
25 | import static com.exozet.android.core.utils.ViewExtensions.getContentRoot;
26 |
27 | public class MainActivity extends AppCompatActivity {
28 |
29 | private static final java.lang.String TAG = MainActivity.class.getSimpleName();
30 |
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_main);
35 |
36 | // Keep the screen always on
37 | if (BuildConfig.DEBUG)
38 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
39 |
40 | Menu.with(this);
41 |
42 | FragmentExtensions.replace(new BuildFragment());
43 |
44 | new GPVersionChecker.Builder(this)
45 | .setCheckingStrategy(CheckingStrategy.ALWAYS)
46 | .showDialog(true)
47 | // .forceUpdate(BuildConfig.RELEASE)
48 | // .setCustomPackageName("net.kibotu.android.deviceinfo")
49 | .setVersionInfoListener(version -> {
50 | Logger.v(TAG, "version=" + version);
51 | })
52 | .create();
53 | }
54 |
55 | @Override
56 | protected void onResume() {
57 | super.onResume();
58 | final TextView title = getContentRoot().findViewById(R.id.actionbar_title);
59 | title.setTextColor(getResColor(R.color.white));
60 | }
61 |
62 | @Override
63 | public void onConfigurationChanged(Configuration newConfig) {
64 | super.onConfigurationChanged(newConfig);
65 | Logger.v(TAG, "[onConfigurationChanged] " + newConfig);
66 | }
67 |
68 | @Override
69 | protected void attachBaseContext(Context newBase) {
70 | super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
71 | }
72 |
73 | @Override
74 | public void onBackPressed() {
75 |
76 | // hide keyboard
77 | DeviceExtensions.hideKeyboard();
78 |
79 | // close menu
80 | if (Menu.isDrawerOpen()) {
81 | Menu.closeDrawer();
82 | return;
83 | }
84 |
85 | if (BaseFragment.onBackPressed())
86 | return;
87 |
88 | // pop back stack
89 | if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
90 | getSupportFragmentManager().popBackStack();
91 | FragmentExtensions.printBackStack();
92 | return;
93 | }
94 |
95 | // quit app
96 | finish();
97 | }
98 |
99 | @Override
100 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
101 | super.onActivityResult(requestCode, resultCode, data);
102 | final Fragment fragment = currentFragment(R.id.fragment_container);
103 | if (fragment != null)
104 | fragment.onActivityResult(requestCode, resultCode, data);
105 | }
106 |
107 | @CallSuper
108 | @Override
109 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
110 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/lib/src/main/java/net/kibotu/android/deviceinfo/library/buildinfo/DeviceUuidFactory.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo.library.buildinfo;
2 |
3 | import android.Manifest;
4 | import android.content.Context;
5 | import android.content.SharedPreferences;
6 | import android.provider.Settings;
7 | import androidx.annotation.NonNull;
8 | import android.telephony.TelephonyManager;
9 |
10 | import androidx.annotation.RequiresPermission;
11 |
12 | import java.io.UnsupportedEncodingException;
13 | import java.util.UUID;
14 |
15 | /**
16 | * credits go to http://stackoverflow.com/a/5626208/957370
17 | */
18 | public class DeviceUuidFactory {
19 |
20 | protected static final String PREFS_FILE = "device_id.xml";
21 | protected static final String PREFS_DEVICE_ID = "device_id";
22 | protected volatile static UUID uuid;
23 |
24 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
25 | public DeviceUuidFactory(@NonNull final Context context) {
26 | if (uuid != null) {
27 | return;
28 | }
29 | synchronized (DeviceUuidFactory.class) {
30 | if (uuid != null) {
31 | return;
32 | }
33 | final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);
34 | final String id = prefs.getString(PREFS_DEVICE_ID, null);
35 | if (id != null) {
36 | // Use the ids previously computed and stored in the
37 | // prefs file
38 | uuid = UUID.fromString(id);
39 | } else {
40 | final String androidId = Settings.Secure.getString(
41 | context.getContentResolver(), Settings.Secure.ANDROID_ID);
42 | // Use the Android ID unless it's broken, in which case
43 | // fallback on deviceId,
44 | // unless it's not available, then fallback on a random
45 | // number which we store to a prefs file
46 | try {
47 | if (!"9774d56d682e549c".equals(androidId)) {
48 | uuid = UUID.nameUUIDFromBytes(androidId
49 | .getBytes("utf8"));
50 | } else {
51 | final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
52 | uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
53 | }
54 | } catch (@NonNull final UnsupportedEncodingException e) {
55 | throw new RuntimeException(e);
56 | }
57 | // Write the value out to the prefs file
58 | prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).apply();
59 | }
60 | }
61 | }
62 |
63 | /**
64 | * Returns a unique UUID for the current android device. As with all UUIDs,
65 | * this unique ID is "very highly likely" to be unique across all Android
66 | * devices. Much more so than ANDROID_ID is.
67 | *
68 | * The UUID is generated by using ANDROID_ID as the base key if appropriate,
69 | * falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to
70 | * be incorrect, and finally falling back on a random UUID that's persisted
71 | * to SharedPreferences if getDeviceID() does not return a usable value.
72 | *
73 | * In some rare circumstances, this ID may change. In particular, if the
74 | * device is factory reset a new device ID may be generated. In addition, if
75 | * a user upgrades their phone from certain buggy implementations of Android
76 | * 2.2 to a newer, non-buggy version of Android, the device ID may change.
77 | * Or, if a user uninstalls your app on a device that has neither a proper
78 | * Android ID nor a Device ID, this ID may change on reinstallation.
79 | *
80 | * Note that if the code falls back on using TelephonyManager.getDeviceId(),
81 | * the resulting ID will NOT change after a factory reset. Something to be
82 | * aware of.
83 | *
84 | * Works around a bug in Android 2.2 for many devices when using ANDROID_ID
85 | * directly.
86 | *
87 | * @return a UUID that may be used to uniquely identify your device for most
88 | * purposes.
89 | * @see http://code.google.com/p/android/issues/detail?id=10603
90 | */
91 | public UUID getDeviceUuid() {
92 | return uuid;
93 | }
94 | }
--------------------------------------------------------------------------------
/app/src/main/java/net/kibotu/android/deviceinfo/ui/list/ListFragment.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo.ui.list;
2 |
3 | import android.os.Bundle;
4 | import androidx.annotation.NonNull;
5 | import androidx.annotation.Nullable;
6 | import android.support.v7.widget.LinearLayoutManager;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.view.View;
9 | import android.view.animation.OvershootInterpolator;
10 |
11 | import androidx.recyclerview.widget.RecyclerView;
12 | import com.common.android.utils.interfaces.TitleProvider;
13 |
14 | import net.kibotu.android.deviceinfo.R;
15 | import net.kibotu.android.deviceinfo.model.ListItem;
16 | import net.kibotu.android.deviceinfo.ui.BaseFragment;
17 | import net.kibotu.android.deviceinfo.ui.list.binder.CardViewHorizontalListItemBinder;
18 | import net.kibotu.android.deviceinfo.ui.list.binder.CardViewSubListItemBinder;
19 | import net.kibotu.android.deviceinfo.ui.list.binder.VerticalListItemBinderCardView;
20 | import net.kibotu.android.deviceinfo.ui.menu.Menu;
21 | import net.kibotu.android.recyclerviewpresenter.PresenterAdapter;
22 |
23 | import butterknife.BindView;
24 | import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
25 | import jp.wasabeef.recyclerview.animators.LandingAnimator;
26 | import me.everything.android.ui.overscroll.OverScrollDecoratorHelper;
27 |
28 | import static android.text.TextUtils.isEmpty;
29 |
30 | /**
31 | * Created by Nyaruhodo on 20.02.2016.
32 | */
33 | public abstract class ListFragment extends BaseFragment {
34 |
35 | @NonNull
36 | @BindView(R.id.list)
37 | RecyclerView list;
38 | protected PresenterAdapter adapter;
39 |
40 | @Override
41 | public int getLayout() {
42 | return R.layout.fragment_list;
43 | }
44 |
45 | @Override
46 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
47 | super.onViewCreated(view, savedInstanceState);
48 |
49 | Menu.setActionBarIcon(getHomeIcon());
50 |
51 | setListItemAnimator();
52 |
53 | adapter = new PresenterAdapter<>();
54 | list.setAdapter(injectAdapterAnimation(adapter));
55 | }
56 |
57 | protected RecyclerView.Adapter injectAdapterAnimation(RecyclerView.Adapter adapter) {
58 | final ScaleInAnimationAdapter animationAdapter = new ScaleInAnimationAdapter(adapter, 0.90f);
59 | animationAdapter.setDuration(200);
60 | animationAdapter.setFirstOnly(false);
61 | animationAdapter.setInterpolator(new OvershootInterpolator(1f));
62 | return animationAdapter;
63 | }
64 |
65 | private void setListItemAnimator() {
66 | OverScrollDecoratorHelper.setUpOverScroll(list, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);
67 | list.setItemAnimator(new LandingAnimator(new OvershootInterpolator(1f)));
68 | list.getItemAnimator().setAddDuration(125);
69 | list.getItemAnimator().setRemoveDuration(125);
70 | list.getItemAnimator().setMoveDuration(125);
71 | list.getItemAnimator().setChangeDuration(125);
72 | }
73 |
74 | protected void notifyDataSetChanged() {
75 | adapter.notifyDataSetChanged();
76 | }
77 |
78 | protected void clear() {
79 | adapter.clear();
80 | }
81 |
82 | protected void addHorizontallyCard(String label, Object value, String description) {
83 | final String content = String.valueOf(value);
84 |
85 | if (isEmpty(content))
86 | return;
87 |
88 | adapter.append(new ListItem()
89 | .setLabel(label)
90 | .setValue(content)
91 | .setDescription(description),
92 | CardViewHorizontalListItemBinder.class);
93 | }
94 |
95 | protected void addVerticallyCard(String label, Object value, String description) {
96 | final String content = String.valueOf(value);
97 |
98 | if (isEmpty(content))
99 | return;
100 |
101 | adapter.append(new ListItem()
102 | .setLabel(label)
103 | .setValue(String.valueOf(value))
104 | .setDescription(description),
105 | VerticalListItemBinderCardView.class);
106 | }
107 |
108 | public void addSubListItem(ListItem item) {
109 | if (item == null)
110 | return;
111 |
112 | adapter.add(item, CardViewSubListItemBinder.class);
113 | }
114 |
115 | protected abstract int getHomeIcon();
116 |
117 | @Override
118 | protected void onActiveAfterBackStackChanged() {
119 | super.onActiveAfterBackStackChanged();
120 |
121 | Menu.setActionBarIcon(getHomeIcon());
122 | Menu.setActionBarTitle(getTitle());
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/lib/src/main/java/net/kibotu/android/deviceinfo/library/network/Connectivity.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo.library.network;
2 |
3 | import android.net.ConnectivityManager;
4 | import android.net.NetworkInfo;
5 | import android.telephony.TelephonyManager;
6 |
7 | import static net.kibotu.android.deviceinfo.library.services.SystemService.getConnectivityManager;
8 |
9 | /**
10 | * Check device's network connectivity and speed
11 | *
12 | * @author emil http://stackoverflow.com/users/220710/emil
13 | */
14 | public class Connectivity {
15 |
16 | /**
17 | * Get the network info
18 | *
19 | * @return NetworkInfo
20 | */
21 | public static NetworkInfo getNetworkInfo() {
22 | return getConnectivityManager().getActiveNetworkInfo();
23 | }
24 |
25 | /**
26 | * Check if there is any connectivity
27 | *
28 | * @return true if connected
29 | */
30 | public static boolean isConnected() {
31 | NetworkInfo info = Connectivity.getNetworkInfo();
32 | return (info != null && info.isConnected());
33 | }
34 |
35 | /**
36 | * Check if there is any connectivity to a Wifi network
37 | *
38 | * @param type
39 | * @return
40 | */
41 | public static boolean isConnectedWifi() {
42 | NetworkInfo info = Connectivity.getNetworkInfo();
43 | return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
44 | }
45 |
46 | /**
47 | * Check if there is any connectivity to a mobile network
48 | *
49 | * @param type
50 | * @return
51 | */
52 | public static boolean isConnectedMobile() {
53 | NetworkInfo info = Connectivity.getNetworkInfo();
54 | return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
55 | }
56 |
57 | /**
58 | * Check if there is fast connectivity
59 | *
60 | * @return
61 | */
62 | public static boolean isConnectedFast() {
63 | NetworkInfo info = Connectivity.getNetworkInfo();
64 | return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(), info.getSubtype()));
65 | }
66 |
67 | /**
68 | * Check if the connection is fast
69 | *
70 | * @param type
71 | * @param subType
72 | * @return
73 | */
74 | public static boolean isConnectionFast(int type, int subType) {
75 | if (type == ConnectivityManager.TYPE_WIFI) {
76 | return true;
77 | } else if (type == ConnectivityManager.TYPE_MOBILE) {
78 | switch (subType) {
79 | case TelephonyManager.NETWORK_TYPE_1xRTT:
80 | return false; // ~ 50-100 kbps
81 | case TelephonyManager.NETWORK_TYPE_CDMA:
82 | return false; // ~ 14-64 kbps
83 | case TelephonyManager.NETWORK_TYPE_EDGE:
84 | return false; // ~ 50-100 kbps
85 | case TelephonyManager.NETWORK_TYPE_EVDO_0:
86 | return true; // ~ 400-1000 kbps
87 | case TelephonyManager.NETWORK_TYPE_EVDO_A:
88 | return true; // ~ 600-1400 kbps
89 | case TelephonyManager.NETWORK_TYPE_GPRS:
90 | return false; // ~ 100 kbps
91 | case TelephonyManager.NETWORK_TYPE_HSDPA:
92 | return true; // ~ 2-14 Mbps
93 | case TelephonyManager.NETWORK_TYPE_HSPA:
94 | return true; // ~ 700-1700 kbps
95 | case TelephonyManager.NETWORK_TYPE_HSUPA:
96 | return true; // ~ 1-23 Mbps
97 | case TelephonyManager.NETWORK_TYPE_UMTS:
98 | return true; // ~ 400-7000 kbps
99 | /*
100 | * Above API level 7, make sure to set android:targetSdkVersion
101 | * to appropriate level to use these
102 | */
103 | case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
104 | return true; // ~ 1-2 Mbps
105 | case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
106 | return true; // ~ 5 Mbps
107 | case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
108 | return true; // ~ 10-20 Mbps
109 | case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
110 | return false; // ~25 kbps
111 | case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
112 | return true; // ~ 10+ Mbps
113 | // Unknown
114 | case TelephonyManager.NETWORK_TYPE_UNKNOWN:
115 | default:
116 | return false;
117 | }
118 | } else {
119 | return false;
120 | }
121 | }
122 |
123 | }
--------------------------------------------------------------------------------
/lib/src/main/java/net/kibotu/android/deviceinfo/library/cpu/Core.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo.library.cpu;
2 |
3 | /**
4 | * Created by Nyaruhodo on 06.03.2016.
5 | */
6 | public class Core {
7 |
8 | /**
9 | * normal processes executing in user mode
10 | */
11 | public int user;
12 | /**
13 | * niced processes executing in user mode
14 | */
15 | public int nice;
16 | /**
17 | * processes executing in kernel mode
18 | */
19 | public int system;
20 | /**
21 | * twiddling thumbs
22 | */
23 | public int idle;
24 | /**
25 | * waiting for I/O to complete
26 | */
27 | public int iowait;
28 | /**
29 | * servicing public interrupts
30 | */
31 | public int irq;
32 | /**
33 | * servicing softirqs
34 | */
35 | public int softirq;
36 | /**
37 | * involuntary wait
38 | */
39 | public int steal;
40 | /**
41 | * running a normal guest
42 | */
43 | public int guest;
44 | /**
45 | * running a niced guest
46 | */
47 | public int guest_nice;
48 |
49 | /**
50 | * computed difference between previous and current core usage
51 | */
52 | public float usage;
53 |
54 | public int total() {
55 | return user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice;
56 | }
57 |
58 | public int getUser() {
59 | return user;
60 | }
61 |
62 | public Core setUser(int user) {
63 | this.user = user;
64 | return this;
65 | }
66 |
67 | public int getNice() {
68 | return nice;
69 | }
70 |
71 | public Core setNice(int nice) {
72 | this.nice = nice;
73 | return this;
74 | }
75 |
76 | public int getSystem() {
77 | return system;
78 | }
79 |
80 | public Core setSystem(int system) {
81 | this.system = system;
82 | return this;
83 | }
84 |
85 | public int getIdle() {
86 | return idle;
87 | }
88 |
89 | public Core setIdle(int idle) {
90 | this.idle = idle;
91 | return this;
92 | }
93 |
94 | public int getIowait() {
95 | return iowait;
96 | }
97 |
98 | public Core setIowait(int iowait) {
99 | this.iowait = iowait;
100 | return this;
101 | }
102 |
103 | public int getIrq() {
104 | return irq;
105 | }
106 |
107 | public Core setIrq(int irq) {
108 | this.irq = irq;
109 | return this;
110 | }
111 |
112 | public int getSoftirq() {
113 | return softirq;
114 | }
115 |
116 | public Core setSoftirq(int softirq) {
117 | this.softirq = softirq;
118 | return this;
119 | }
120 |
121 | public int getSteal() {
122 | return steal;
123 | }
124 |
125 | public Core setSteal(int steal) {
126 | this.steal = steal;
127 | return this;
128 | }
129 |
130 | public int getGuest() {
131 | return guest;
132 | }
133 |
134 | public Core setGuest(int guest) {
135 | this.guest = guest;
136 | return this;
137 | }
138 |
139 | public int getGuest_nice() {
140 | return guest_nice;
141 | }
142 |
143 | public Core setGuest_nice(int guest_nice) {
144 | this.guest_nice = guest_nice;
145 | return this;
146 | }
147 |
148 | @Override
149 | public boolean equals(Object o) {
150 | if (this == o) return true;
151 | if (o == null || getClass() != o.getClass()) return false;
152 |
153 | Core core = (Core) o;
154 |
155 | if (user != core.user) return false;
156 | if (nice != core.nice) return false;
157 | if (system != core.system) return false;
158 | if (idle != core.idle) return false;
159 | if (iowait != core.iowait) return false;
160 | if (irq != core.irq) return false;
161 | if (softirq != core.softirq) return false;
162 | if (steal != core.steal) return false;
163 | if (guest != core.guest) return false;
164 | if (guest_nice != core.guest_nice) return false;
165 | return Float.compare(core.usage, usage) == 0;
166 |
167 | }
168 |
169 | @Override
170 | public int hashCode() {
171 | int result = user;
172 | result = 31 * result + nice;
173 | result = 31 * result + system;
174 | result = 31 * result + idle;
175 | result = 31 * result + iowait;
176 | result = 31 * result + irq;
177 | result = 31 * result + softirq;
178 | result = 31 * result + steal;
179 | result = 31 * result + guest;
180 | result = 31 * result + guest_nice;
181 | result = 31 * result + (usage != +0.0f ? Float.floatToIntBits(usage) : 0);
182 | return result;
183 | }
184 |
185 | @Override
186 | public String toString() {
187 | return "Core{" +
188 | "user=" + user +
189 | ", nice=" + nice +
190 | ", system=" + system +
191 | ", idle=" + idle +
192 | ", iowait=" + iowait +
193 | ", irq=" + irq +
194 | ", softirq=" + softirq +
195 | ", steal=" + steal +
196 | ", guest=" + guest +
197 | ", guest_nice=" + guest_nice +
198 | ", usage=" + usage +
199 | '}';
200 | }
201 | }
202 |
--------------------------------------------------------------------------------
/lib/src/main/java/net/kibotu/android/deviceinfo/library/misc/ReflectionHelper.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo.library.misc;
2 |
3 | import java.lang.reflect.Field;
4 | import java.lang.reflect.InvocationTargetException;
5 | import java.lang.reflect.Method;
6 |
7 | final public class ReflectionHelper {
8 |
9 | private ReflectionHelper() throws IllegalAccessException {
10 | throw new IllegalAccessException("static class");
11 | }
12 |
13 | public static String getSystemProperty(final String propName) {
14 | Class> clsSystemProperties = tryClassForName("android.os.SystemProperties");
15 | Method mtdGet = tryGetMethod(clsSystemProperties, "get", String.class);
16 | return tryInvoke(mtdGet, null, propName);
17 | }
18 |
19 | public static Class> tryClassForName(final String className) {
20 | try {
21 | return Class.forName(className);
22 | } catch (final ClassNotFoundException e) {
23 | e.printStackTrace();
24 | return null;
25 | }
26 | }
27 |
28 | public static Method tryGetMethod(final Class> cls, final String name, final Class>... parameterTypes) {
29 | try {
30 | return cls.getDeclaredMethod(name, parameterTypes);
31 | } catch (final Exception e) {
32 | e.printStackTrace();
33 | return null;
34 | }
35 | }
36 |
37 | public static T tryInvoke(final Method m, final Object object, final Object... params) {
38 | try {
39 | return (T) m.invoke(object, params);
40 | } catch (final InvocationTargetException e) {
41 | throw new RuntimeException(e.getTargetException());
42 | } catch (final Exception e) {
43 | e.printStackTrace();
44 | return null;
45 | }
46 | }
47 |
48 | public static T get(final Class> cls, final String method, final Object target, final Object... params) {
49 | Method m = tryGetMethod(cls, method);
50 | return tryInvoke(m, target, params);
51 | }
52 |
53 | public static T getPublicStaticField(final Class> cls, final String method) {
54 |
55 | T result = null;
56 |
57 | try {
58 | final Field field = cls.getField(method);
59 | result = (T) field.get(null);
60 | } catch (final NoSuchFieldException e) {
61 | e.printStackTrace();
62 | } catch (final IllegalAccessException e) {
63 | e.printStackTrace();
64 | }
65 |
66 | return result;
67 | }
68 |
69 | public static T tryInvokePublicStatic(String cls, String method, final ReflectionHelperParamater params) {
70 | T t = null;
71 | try {
72 | final Method m = tryClassForName(cls).getMethod(method, params.types);
73 | t = (T) m.invoke(null, params.values);
74 | } catch (final NoSuchMethodException e) {
75 | e.printStackTrace();
76 | } catch (InvocationTargetException e) {
77 | e.printStackTrace();
78 | } catch (IllegalAccessException e) {
79 | e.printStackTrace();
80 | }
81 | return t;
82 | }
83 |
84 | public static Object contruct(final String cls) {
85 | try {
86 | return ReflectionHelper.tryClassForName(cls).getConstructor(String.class).newInstance("Throwable");
87 | } catch (final InstantiationException e) {
88 | e.printStackTrace();
89 | } catch (final IllegalAccessException e) {
90 | e.printStackTrace();
91 | } catch (final InvocationTargetException e) {
92 | e.printStackTrace();
93 | } catch (final NoSuchMethodException e) {
94 | e.printStackTrace();
95 | }
96 | return null;
97 | }
98 |
99 | public static void tryInvoke(final Class cls, final Object object, final String method, final ReflectionHelperParamater params) {
100 | Method m = ReflectionHelper.tryGetMethod(cls, method, params.types);
101 | try {
102 | m.invoke(object, params.values);
103 | } catch (final IllegalAccessException e) {
104 | e.printStackTrace();
105 | } catch (final InvocationTargetException e) {
106 | e.printStackTrace();
107 | }
108 | }
109 |
110 | public static Object contruct(final Class cls, final ReflectionHelperParamater params) {
111 | try {
112 | return cls.getConstructor(params.types).newInstance(params.values);
113 | } catch (final InstantiationException e) {
114 | e.printStackTrace();
115 | } catch (final IllegalAccessException e) {
116 | e.printStackTrace();
117 | } catch (final InvocationTargetException e) {
118 | e.printStackTrace();
119 | } catch (final NoSuchMethodException e) {
120 | e.printStackTrace();
121 | }
122 | return null;
123 | }
124 |
125 | public static void tryInvoke(final Class cls, final Object obj, final String method) {
126 | tryInvoke(tryGetMethod(cls, method, null), obj);
127 | }
128 |
129 | final static public class ReflectionHelperParamater {
130 | public final Class>[] types;
131 | public final Object[] values;
132 |
133 | public ReflectionHelperParamater(final Class>[] types, final Object[] values) {
134 | this.types = types;
135 | this.values = values;
136 | }
137 | }
138 | }
--------------------------------------------------------------------------------
/lib/src/main/java/net/kibotu/android/deviceinfo/library/cpu/Cpu.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo.library.cpu;
2 |
3 | import java.io.File;
4 | import java.util.ArrayList;
5 |
6 | /**
7 | * Created by Nyaruhodo on 06.03.2016.
8 | */
9 | public class Cpu {
10 |
11 | /**
12 | * Measures the number of jiffies (1/100 of a second for x86 systems) that the system has been in user mode,
13 | * user mode with low priority (nice), system mode, idle task, I/O wait, IRQ (hardirq), and softirq respectively.
14 | * The IRQ (hardirq) is the direct response to a hardware event. The IRQ takes minimal work for queuing the "heavy" work up for the softirq to execute.
15 | * The softirq runs at a lower priority than the IRQ and therefore may be interrupted more frequently.
16 | * The total for all CPUs is given at the top, while each individual CPU is listed below with its own statistics.
17 | * The following example is a 4-way Intel Pentium Xeon configuration with multi-threading enabled,
18 | * therefore showing four physical processors and four virtual processors totaling eight processors.
19 | */
20 | public Core allCores;
21 |
22 | private static int amountCoresCache = -1;
23 |
24 | public ArrayList cores;
25 |
26 | /**
27 | * context switches across all CPUs.
28 | */
29 | public int ctxt;
30 |
31 | /**
32 | * time at which the system booted in epoch unix time in seconds
33 | */
34 | public int btime;
35 |
36 | /**
37 | * number of processes and threads created
38 | */
39 | public int processes;
40 |
41 | /**
42 | * number of processes currently running on CPUs
43 | */
44 | public int procs_running;
45 |
46 | /**
47 | * processes currently blocked
48 | */
49 | public int procs_blocked;
50 |
51 | public Cpu() {
52 | cores = new ArrayList<>();
53 | }
54 |
55 | public ArrayList getCores() {
56 | return cores;
57 | }
58 |
59 | public int getCtxt() {
60 | return ctxt;
61 | }
62 |
63 | public int getBtimeAsEpoch() {
64 | return btime;
65 | }
66 |
67 | public int getProcesses() {
68 | return processes;
69 | }
70 |
71 | public int getProcs_running() {
72 | return procs_running;
73 | }
74 |
75 | public int getProcs_blocked() {
76 | return procs_blocked;
77 | }
78 |
79 | public Core getAllCores() {
80 | return allCores;
81 | }
82 |
83 | public void setAllCores(Core allCores) {
84 | this.allCores = allCores;
85 | }
86 |
87 | public void setCores(ArrayList cores) {
88 | this.cores = cores;
89 | }
90 |
91 | public void setCtxt(int ctxt) {
92 | this.ctxt = ctxt;
93 | }
94 |
95 | public void setBtime(int btime) {
96 | this.btime = btime;
97 | }
98 |
99 | public void setProcesses(int processes) {
100 | this.processes = processes;
101 | }
102 |
103 | public void setProcs_running(int procs_running) {
104 | this.procs_running = procs_running;
105 | }
106 |
107 | public void setProcs_blocked(int procs_blocked) {
108 | this.procs_blocked = procs_blocked;
109 | }
110 |
111 | /**
112 | * Gets the number of cores available in this device, across all processors.
113 | * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
114 | *
115 | * @return The number of cores, or 1 if failed to get result
116 | */
117 | public static int getAmountCores() {
118 | if (amountCoresCache != -1)
119 | return amountCoresCache;
120 |
121 | try {
122 | //Get directory containing CPU info
123 | File dir = new File("/sys/devices/system/cpu/");
124 | //Filter to only list the devices we care about
125 | final File[] files = dir.listFiles(new CpuFileFilter());
126 | //Return the number of cores (virtual CPU devices)
127 | return amountCoresCache = files.length;
128 | } catch (final Exception e) {
129 | e.printStackTrace();
130 | //Default to return 1 core
131 | return 1;
132 | }
133 | }
134 |
135 | @Override
136 | public boolean equals(Object o) {
137 | if (this == o) return true;
138 | if (o == null || getClass() != o.getClass()) return false;
139 |
140 | Cpu cpu = (Cpu) o;
141 |
142 | if (ctxt != cpu.ctxt) return false;
143 | if (btime != cpu.btime) return false;
144 | if (processes != cpu.processes) return false;
145 | if (procs_running != cpu.procs_running) return false;
146 | if (procs_blocked != cpu.procs_blocked) return false;
147 | if (allCores != null ? !allCores.equals(cpu.allCores) : cpu.allCores != null) return false;
148 | return cores != null ? cores.equals(cpu.cores) : cpu.cores == null;
149 |
150 | }
151 |
152 | @Override
153 | public int hashCode() {
154 | int result = allCores != null ? allCores.hashCode() : 0;
155 | result = 31 * result + (cores != null ? cores.hashCode() : 0);
156 | result = 31 * result + ctxt;
157 | result = 31 * result + btime;
158 | result = 31 * result + processes;
159 | result = 31 * result + procs_running;
160 | result = 31 * result + procs_blocked;
161 | return result;
162 | }
163 |
164 | @Override
165 | public String toString() {
166 | return "Cpu{" +
167 | "allCores=" + allCores +
168 | ", cores=" + cores +
169 | ", ctxt=" + ctxt +
170 | ", btime=" + btime +
171 | ", processes=" + processes +
172 | ", procs_running=" + procs_running +
173 | ", procs_blocked=" + procs_blocked +
174 | '}';
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/lib/src/main/java/net/kibotu/android/deviceinfo/library/display/Display.java:
--------------------------------------------------------------------------------
1 | package net.kibotu.android.deviceinfo.library.display;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.pm.ActivityInfo;
5 | import android.content.res.Configuration;
6 | import android.graphics.Point;
7 | import android.os.Build;
8 | import android.util.DisplayMetrics;
9 |
10 | import androidx.annotation.NonNull;
11 | import net.kibotu.android.deviceinfo.library.R;
12 |
13 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
14 | import static java.lang.Math.max;
15 | import static java.lang.Math.min;
16 | import static java.lang.Math.sqrt;
17 | import static net.kibotu.android.deviceinfo.library.Device.getContext;
18 | import static net.kibotu.android.deviceinfo.library.services.SystemService.getWindowManager;
19 | import static net.kibotu.android.deviceinfo.library.version.Version.isAtLeastVersion;
20 |
21 | public class Display {
22 |
23 | public static android.view.Display getDefaultDisplay() {
24 | return getWindowManager().getDefaultDisplay();
25 | }
26 |
27 | public static float getRefreshRate() {
28 | return getDefaultDisplay().getRefreshRate();
29 | }
30 |
31 | @NonNull
32 | public static DisplayMetrics getDisplayMetrics() {
33 | final DisplayMetrics metrics = new DisplayMetrics();
34 | getDefaultDisplay().getMetrics(metrics);
35 | return metrics;
36 | }
37 |
38 |
39 | @TargetApi(JELLY_BEAN_MR1)
40 | @NonNull
41 | public static DisplayMetrics getRealDisplayMetrics() {
42 | final DisplayMetrics metrics = new DisplayMetrics();
43 | getDefaultDisplay().getRealMetrics(metrics);
44 | return metrics;
45 | }
46 |
47 | public static double getScreenDiagonalAsPixel() {
48 | final Dimension screen = getScreenDimensions();
49 | return sqrt(screen.width * screen.width + screen.height * screen.height);
50 | }
51 |
52 | public static double getScreenDiagonalAsInch() {
53 | final Dimension screen = getScreenDimensions();
54 | final DisplayMetrics displayMetrics = getDisplayMetrics();
55 | return sqrt(screen.width / displayMetrics.xdpi * screen.width / displayMetrics.xdpi
56 | + screen.height / displayMetrics.ydpi * screen.height / displayMetrics.ydpi);
57 | }
58 |
59 | public static boolean hasSoftKeys() {
60 | return getSoftKeyHeight() > 0;
61 | }
62 |
63 | public static boolean isTabletByLayout() {
64 | return getContext().getResources().getBoolean(R.bool.IsTablet);
65 | }
66 |
67 | /**
68 | * determine-if-the-device-is-a-smartphone-or-tablet
69 | */
70 | public static boolean isTablet() {
71 | return (getContext().getResources().getConfiguration().screenLayout
72 | & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
73 | }
74 |
75 | public static int getOrientation() {
76 | return isTablet()
77 | ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
78 | : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
79 | }
80 |
81 | public static int getStatusBarHeight() {
82 | int result = 0;
83 | int resourceId = getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
84 | if (resourceId > 0) {
85 | result = getContext().getResources().getDimensionPixelSize(resourceId);
86 | }
87 | return result;
88 | }
89 |
90 | public static String getDisplayCountry() {
91 | return getContext().getResources().getConfiguration().locale.getDisplayCountry();
92 | }
93 |
94 | public static int getUsableScreenHeight() {
95 | return getScreenDimensions().height - getStatusBarHeight() - getSoftKeyHeight();
96 | }
97 |
98 | public static int getSoftKeyHeight() {
99 | return isAtLeastVersion(JELLY_BEAN_MR1)
100 | ? getRealDisplayMetrics().heightPixels - getDisplayMetrics().heightPixels
101 | : getScreenDimensions().height - getDisplayMetrics().heightPixels;
102 | }
103 |
104 | @NonNull
105 | public static Dimension getScreenDimensionsPortrait() {
106 | final Dimension dimension = getScreenDimensions();
107 | return new Dimension(min(dimension.width, dimension.height), max(dimension.width, dimension.height));
108 | }
109 |
110 | @NonNull
111 | public static Dimension getScreenDimensionsLandscape() {
112 | final Dimension dimension = getScreenDimensions();
113 | return new Dimension(max(dimension.width, dimension.height), min(dimension.width, dimension.height));
114 | }
115 |
116 | @NonNull
117 | public static Dimension getScreenDimensions() {
118 |
119 | final DisplayMetrics dm = new DisplayMetrics();
120 | final android.view.Display display = getDefaultDisplay();
121 | display.getMetrics(dm);
122 |
123 | int screenWidth = dm.widthPixels;
124 | int screenHeight = dm.heightPixels;
125 |
126 | if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
127 | try {
128 | screenWidth = (Integer) android.view.Display.class.getMethod("getRawWidth").invoke(display);
129 | screenHeight = (Integer) android.view.Display.class.getMethod("getRawHeight").invoke(display);
130 | } catch (Exception ignored) {
131 | }
132 | }
133 | if (Build.VERSION.SDK_INT >= 17) {
134 | try {
135 | Point realSize = new Point();
136 | android.view.Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
137 | screenWidth = realSize.x;
138 | screenHeight = realSize.y;
139 | } catch (Exception ignored) {
140 | }
141 | }
142 |
143 | return new Dimension(screenWidth, screenHeight);
144 | }
145 | }
--------------------------------------------------------------------------------