Java/Android Displaying .apk files as files but with their icons - android

I am building a root app allowing to debloat apk files from system/app and system/priv app. I have a ListView that takes the content of both dirs and filters them against a string array that I built (not all apps can be debloated). It feeds into a list with multiple choice mode.
Now, what I can't manage is how to display apk files with their icons.
I am using a simple adapter for now. But I can switch to base adapter. What I need is a way to add the app icons into the Image View.
I am not using the package manager to generate the list, for several reasons I need it to be the actual apk files that are displayed.
Can someone please help me with a way to make an apk file display with it's icon? Like in root explorer f.e.?
This is the main part of code that takes care of the list for now:
public class MainActivity extends ListActivity
implements View.OnClickListener,
AdapterView.OnItemSelectedListener{
private List<String> fileList = new ArrayList<String>();
Button debloat;
ListView appList;
File app = new File("/system/app");
File privApp = new File("/system/priv-app");
ArrayAdapter<String> directoryList;
BufferedWriter debloatBW;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
setContentView(R.layout.activity_main);
debloat = (Button) findViewById(R.id.button);
debloat.setOnClickListener(this);
appList = getListView();
File[] files = app.listFiles();
File[] files1 = privApp.listFiles();
List<String> relevantApps =
Arrays.asList(getResources().getStringArray(R.array.include_apk));
fileList.clear();
for (File file : files){
if (file.isFile()
&& file.getPath().endsWith(".apk")
&& relevantApps.contains(file.getName().toString()))
{
fileList.add(file.getName());
}
}
for (File file1 : files1) {
if (file1.isFile()
&& file1.getPath().endsWith(".apk")
&& relevantApps.contains(file1.getName().toString()))
{
fileList.add(file1.getName());
}
}
directoryList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, fileList);
appList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
appList.setAdapter(directoryList);
}
Thank you for any advice you might have!
Cheers!

I recommend creating your own custome adapter in which you inflate the row as usuala and then set the text, to get the icon of the package with the help of this post Get Apk Icon and set it to the ImageView in the row. How to create a custome adapter you find out bets with this tutorial Using Android ListView.

Related

RSS reader unit testing

Build a android simple RSS reader application ,I have simple class "ReaderAppActivity" which use onCreate method as following to parsing a particular RSS site
public class ReaderAppActivity extends Activity {
/**
* This method creates main application view
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.main);
try {
// Create RSS reader
RssReader rssReader = new RssReader("http://rss.cnn.com/rss/edition.rss");
// Get a ListView from main view
ListView itcItems = (ListView) findViewById(R.id.listMainView);
// Create a list adapter
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this,android.R.layout.simple_list_item_1, rssReader.getItems());
// Set list adapter for the ListView
itcItems.setAdapter(adapter);
// Set list view item click listener
itcItems.setOnItemClickListener(new ListListener(rssReader.getItems(), this));
} catch (Exception e) {
Log.e("Reader", e.getMessage());
}
}
Now want to develop a Junit testing really don't understand on
1) which parameter I should taste my app ? for example assertest, timeout
2) For internet base project is there any unit testing tutorial for beginners ?
You need to read this:
https://developer.android.com/training/testing/start/index.html
https://developer.android.com/studio/test/index.html
In the manuals you find answers for your questions. Also it will help to you to understand what you need to test (1st question).

Dynamic Allocation of Array doesn't work for ArrayAdapter while assigning it for Spinner in Android

I am really facing a problem here to create a Spinner widget in Android. The goal is to populate a Spinner with data that i will dynamically retrieve from a source.
Now I am able to create a spinner with a data source that is implicitly declared in the program. But when ever i am trying to fetch the data from a dynamically created array, the apps throws a Force Close.
I will paste some demo examples to explain my problem here!
String[] SSID = new String[15];
String[] Data = {"Captain","America","Hulk","Ironman","Thor"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
addDevDialogue = new Dialog(this);
addDevDialogue.setContentView(R.layout.popup);
concat();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogue();
}
});
}
public void concat()
{
for(int i=0;i<5;i++)
{
SSID[i]=Data[i];
}
}
public void dialogue()
{
addDevDialogue.setTitle("Movies List");
addDevDialogue.setCancelable(true);
addDevDialogue.show();
spinList2 = (Spinner)addDevDialogue.findViewById(R.id.spinner2);
ArrayAdapter<String> listAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, SSID);
spinList2.setAdapter(listAdapter2);
}
The above code throws an error when ever I try to open the dialogue box.
I have tried this same sample with a pre-defined data source in place of "SSID" which yields a error free output!
I cannot understand why 'SSID[]' array doesnt work when I define it to the ArrayAdapter.
Any Insight will help!!!
You are call calling show() before populating adapter so call
addDevDialogue.show();
after
spinList2.setAdapter(listAdapter2);
UPDATE :
Once change size of SSID
String[] SSID = new String[Data.length];
Hope this will helps you.
Your string array String[] Data = {"Captain","America","Hulk","Ironman","Thor"}; is declared with 5 elements (index 0-4)
In your loop you loop 6 times
public void concat()
{
for(int i=0;i<5;i++)
{
SSID[i]=Data[i];
}
}
Which probably causes an Index out of bounds exception. Change your loop to this
public void concat()
{
for(int i=0;i<4;i++)
{
SSID[i]=Data[i];
}
}
#swarna: You are allocating a fixed array of 15 elements, then populating only 5 elements. The array adapter is probably getting tripped with the other 10 elements which have not been initialized. Suggest you make your SSID array to have only 5 elements OR if this is a dynamically determined value, you could keep ArrayList and keep adding to it. Then, when setting up the adapter do this:
YourObjList.add("one")
YourObjList.add("two")
YourObjList.add("three")
String[] SSID = YourObjList.toArray(new YourObjList[YourObjList.size()]);
ArrayAdapter<String> listAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, SSID);
spinList2.setAdapter(listAdapter2);
This will allow variable list sizes.
Hope this helps you

How to use file browser in android

I have an app for uploading files to a server using php!
How can I implement file browser in my app to select which file to be uploaded! ?
Please give me very simple example ! Or simple solution!
if you need to select any type of file there's no simple solution, you'll have to implement a file manager-similar functionality. Here are some tips and samples to get you started:
prerequisites: your have some basic understanding for how Android works; you know how to implement a custom ListView
use ListView to display the files
parametrize your Adapter with an ArrayList<File>
start with the root of external storage:
File root = Environment
.getExternalStorageDirectory();
the following method will list all files and subdirs in a directory:
public static ArrayList<File> getSubfiles(File root) {
File[] mFiles = root.listFiles();
ArrayList<File> files = new ArrayList<File>();
ArrayList<File> dirs = new ArrayList<File>();
ArrayList<File> allData = new ArrayList<File>();
for (int i = 0; i < myFiles.length; i++) {
if (!myFiles[i].isDirectory()) {
files.add(myFiles[i]);
} else {
dirs.add(myFiles[i]);
}
}
Collections.sort(files);
Collections.sort(dirs);
allData.addAll(dirs);
allData.addAll(files);
return allData;
}
using this method you can move "down" the file system by passing any directory to it. To move up, simply use getParentFile() and pass the result to this method.
So you should always pass the result of this method to your Adapter, calling notifyDataSetChanged() afterwards.
That's all, you have a very basic file viewer. Of course you will need to create some layouts and write a bit more of code, the above tips should help you to get started, since as was already mentioned nobody is going to write half of your app for you here on SO. If you have further questions/problems leave a comment and describe your problem.
There are so many solutions for this on google, and here you can't just ask for a full implementation and expect someone to write it here. Here are a couple of good results:
http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/
http://android-er.blogspot.pt/2010/01/implement-simple-file-explorer-in.html
Basically you will have a list dialog that will search for files and directories on your sdcard, then if it is a file (there are methods in File class to check if it is file or directory for example) then choose it and dismiss the dialog, or if it is a directory, change directory and refresh dialog with list of files and directories and so on.
If you only need to select the file path try the Android-Simple-File-Explorer-Library
First show the file explorer:
Intent intent = new Intent(CONTEXT, SimpleFileExplorerActivity);
startActivityForResult(intent, REQUEST_CODE);
And then receive the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(data != null){
String selectedAbsolutePath = data.getStringExtra(SimpleFileExplorerActivity.ON_ACTIVITY_RESULT_KEY);
Toast.makeText(CONTEXT, selectedAbsolutePath, Toast.LENGTH_SHORT).show();
}
}

Get content-scheme Uri of an image via MediaScannerConnectionClient

I have some pictures stored in the external SD card and I want to show their thumbnails in a GridView. I know that the media scanner has created the thumbnails because I can browse my pictures folder with the standard Gallery application but I don't know where those thumbnails are located so I don't know how to add them to my GridView.
I'm trying to get the thumbnails with:
MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(_imageUri.getLastPathSegment()), type, null)
The _imageUri must be a content-schema Uri so my problem is to find a way of converting the file-schema Uris of my images into content-schema Uris. Unfortunately I don't know how to do it. I've seen lots of SO threads recommending to use Uri.parse() but it just doesn't work so I'm looking for a different solution.
My current approach is to use the media scanner for scanning individual files and try to retrieve the content Uri from the onScanCompleted callback. The code is:
public class SimpleMediaScanner implements MediaScannerConnectionClient {
private MediaScannerConnection mMSC;
private File mFile;
private MyAdapter mAdapter;
public SimpleMediaScanner(Context c, File f, MyAdapter a) {
mAdapter = a;
mFile = f;
mMSC = new MediaScannerConnection(c, this);
mMSC.connect();
}
#Override
public void onMediaScannerConnected() {
mMSC.scanFile(mFile.getAbsolutePath(), null);
}
#Override
public void onScanCompleted(String path, Uri uri) {
// Store the content scheme Uri of the scanned file
// in a public field of the adapter
mAdapter.mThumbUri = uri;
mMSC.disconnect();
}
}
I instantiate this class from my extended SimplecursorAdapter:
SimpleMediaScanner sms = new SimpleMediaScanner(mContext, new File(filepath), this);
Unfortunately the returned mAdapter.mThumbUri value is always null. Could somebody tell me what am I doing wrong? TIA
Have you tried the MediaStorage.Images.Thumbnails class? It provides two static getThumbnail(...) methods that should fulfill your needs.

getting list of mp3 files from a sdcard in the order they were added in the card

hi
i am developing an android app using an emulator.
i am using a filter class to filter mp3 songs and adding these to a list songs.
class Mp3Filter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".mp3"));
}
}
public void updateList()
{
File home = new File(MEDIA_PATH);
if (home.listFiles( new Mp3Filter()).length > 0)
{
for (File file : home.listFiles( new Mp3Filter()))
{
songs.add(file.getName());
}
}
}
i want to know is there any order in which new songs gets added in sdcard like
alphabetically or datewise because i am seeing that new songs getting added anywhere
thus each time i am adding any songs i am finding my song list rearranged.
is there any way to get the list of all songs form the sdcard in the order of dates&time
they were added in the card ?
I don't think the File class exposes the creation date, but you could use the lastModified() method to find out when the files were last modified, then sort the list based on that value.

Categories

Resources