diff options
Diffstat (limited to 'src/org/lineageos')
-rw-r--r-- | src/org/lineageos/appdrawer/AppDrawerActivity.java | 128 | ||||
-rw-r--r-- | src/org/lineageos/appdrawer/ApplicationAdapter.java | 78 |
2 files changed, 206 insertions, 0 deletions
diff --git a/src/org/lineageos/appdrawer/AppDrawerActivity.java b/src/org/lineageos/appdrawer/AppDrawerActivity.java new file mode 100644 index 0000000..dd851a1 --- /dev/null +++ b/src/org/lineageos/appdrawer/AppDrawerActivity.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.lineageos.appdrawer; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.ActivityNotFoundException; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.os.AsyncTask; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.GridView; +import android.widget.Toast; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class AppDrawerActivity extends Activity { + private PackageManager packageManager = null; + private List<ApplicationInfo> applist = null; + private ApplicationAdapter listadaptor = null; + private GridView grid; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + packageManager = getPackageManager(); + + new LoadApplications().execute(); + + grid = (GridView)findViewById(R.id.grid); + grid.setAdapter(listadaptor); + grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + ApplicationInfo app = applist.get(position); + try { + Intent intent = packageManager + .getLaunchIntentForPackage(app.packageName); + + if (null != intent) { + startActivity(intent); + } + } catch (ActivityNotFoundException e) { + Toast.makeText(AppDrawerActivity.this, e.getMessage(), + Toast.LENGTH_LONG).show(); + } catch (Exception e) { + Toast.makeText(AppDrawerActivity.this, e.getMessage(), + Toast.LENGTH_LONG).show(); + } + } + }); + } + + private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) { + ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>(); + for (ApplicationInfo info : list) { + try { + if (null != packageManager.getLaunchIntentForPackage(info.packageName)) { + applist.add(info); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + Collections.sort(applist, new ApplicationInfo.DisplayNameComparator(packageManager)); + return applist; + } + + private class LoadApplications extends AsyncTask<Void, Void, Void> { + private ProgressDialog progress = null; + + @Override + protected Void doInBackground(Void... params) { + applist = checkForLaunchIntent(packageManager.getInstalledApplications( + PackageManager.GET_META_DATA)); + listadaptor = new ApplicationAdapter(AppDrawerActivity.this, + R.layout.grid_item, applist); + + return null; + } + + @Override + protected void onCancelled() { + super.onCancelled(); + } + + @Override + protected void onPostExecute(Void result) { + grid.setAdapter(listadaptor); + progress.dismiss(); + super.onPostExecute(result); + } + + @Override + protected void onPreExecute() { + progress = ProgressDialog.show(AppDrawerActivity.this, null, + "Loading application info..."); + super.onPreExecute(); + } + + @Override + protected void onProgressUpdate(Void... values) { + super.onProgressUpdate(values); + } + } +} diff --git a/src/org/lineageos/appdrawer/ApplicationAdapter.java b/src/org/lineageos/appdrawer/ApplicationAdapter.java new file mode 100644 index 0000000..1b761ab --- /dev/null +++ b/src/org/lineageos/appdrawer/ApplicationAdapter.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.lineageos.appdrawer; + +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import java.util.List; + +public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> { + private List<ApplicationInfo> appsList = null; + private Context context; + private PackageManager packageManager; + + public ApplicationAdapter(Context context, int textViewResourceId, + List<ApplicationInfo> appsList) { + super(context, textViewResourceId, appsList); + this.context = context; + this.appsList = appsList; + packageManager = context.getPackageManager(); + } + + @Override + public int getCount() { + return ((null != appsList) ? appsList.size() : 0); + } + + @Override + public ApplicationInfo getItem(int position) { + return ((null != appsList) ? appsList.get(position) : null); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View view = convertView; + if (null == view) { + LayoutInflater layoutInflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + view = layoutInflater.inflate(R.layout.grid_item, null); + } + + ApplicationInfo data = appsList.get(position); + if (null != data) { + TextView appName = (TextView) view.findViewById(R.id.app_name); + ImageView iconview = (ImageView) view.findViewById(R.id.app_icon); + + appName.setText(data.loadLabel(packageManager)); + iconview.setImageDrawable(data.loadIcon(packageManager)); + } + return view; + } +}; |