Skip to content

Commit

Permalink
Merge pull request #8300 from koying/chgdroidicons
Browse files Browse the repository at this point in the history
CHG: [droid] Use best possible icons for app + refactor
  • Loading branch information
MartijnKaijser committed Oct 26, 2015
2 parents e601708 + 501d465 commit 6b91dde
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 81 deletions.
43 changes: 7 additions & 36 deletions xbmc/android/activity/XBMCApp.cpp
Expand Up @@ -65,8 +65,6 @@
#include "android/jni/System.h"
#include "android/jni/ApplicationInfo.h"
#include "android/jni/StatFs.h"
#include "android/jni/BitmapDrawable.h"
#include "android/jni/Bitmap.h"
#include "android/jni/CharSequence.h"
#include "android/jni/URI.h"
#include "android/jni/Cursor.h"
Expand Down Expand Up @@ -530,51 +528,24 @@ std::vector<androidPackage> CXBMCApp::GetApplications()
CJNIList<CJNIApplicationInfo> packageList = GetPackageManager().getInstalledApplications(CJNIPackageManager::GET_ACTIVITIES);
int numPackages = packageList.size();
for (int i = 0; i < numPackages; i++)
{
androidPackage newPackage;
newPackage.packageName = packageList.get(i).packageName;
newPackage.packageLabel = GetPackageManager().getApplicationLabel(packageList.get(i)).toString();
CJNIIntent intent = GetPackageManager().getLaunchIntentForPackage(newPackage.packageName);
{
CJNIIntent intent = GetPackageManager().getLaunchIntentForPackage(packageList.get(i).packageName);
if (!intent && CJNIBuild::SDK_INT >= 21)
intent = GetPackageManager().getLeanbackLaunchIntentForPackage(newPackage.packageName);
intent = GetPackageManager().getLeanbackLaunchIntentForPackage(packageList.get(i).packageName);
if (!intent)
continue;

androidPackage newPackage;
newPackage.packageName = packageList.get(i).packageName;
newPackage.packageLabel = GetPackageManager().getApplicationLabel(packageList.get(i)).toString();
newPackage.icon = packageList.get(i).icon;
m_applications.push_back(newPackage);
}
}

return m_applications;
}

bool CXBMCApp::GetIconSize(const string &packageName, int *width, int *height)
{
JNIEnv* env = xbmc_jnienv();
AndroidBitmapInfo info;
CJNIBitmapDrawable drawable = (CJNIBitmapDrawable)GetPackageManager().getApplicationIcon(packageName);
CJNIBitmap icon(drawable.getBitmap());
AndroidBitmap_getInfo(env, icon.get_raw(), &info);
*width = info.width;
*height = info.height;
return true;
}

bool CXBMCApp::GetIcon(const string &packageName, void* buffer, unsigned int bufSize)
{
void *bitmapBuf = NULL;
JNIEnv* env = xbmc_jnienv();
CJNIBitmapDrawable drawable = (CJNIBitmapDrawable)GetPackageManager().getApplicationIcon(packageName);
CJNIBitmap bitmap(drawable.getBitmap());
AndroidBitmap_lockPixels(env, bitmap.get_raw(), &bitmapBuf);
if (bitmapBuf)
{
memcpy(buffer, bitmapBuf, bufSize);
AndroidBitmap_unlockPixels(env, bitmap.get_raw());
return true;
}
return false;
}

bool CXBMCApp::HasLaunchIntent(const string &package)
{
return GetPackageManager().getLaunchIntentForPackage(package) != NULL;
Expand Down
5 changes: 2 additions & 3 deletions xbmc/android/activity/XBMCApp.h
Expand Up @@ -48,12 +48,13 @@ struct androidIcon
unsigned int width;
unsigned int height;
void *pixels;
};
};

struct androidPackage
{
std::string packageName;
std::string packageLabel;
int icon;
};

class CXBMCApp : public IActivityHandler, public CJNIMainActivity, public CJNIBroadcastReceiver, public CJNIAudioManagerAudioFocusChangeListener
Expand Down Expand Up @@ -96,8 +97,6 @@ class CXBMCApp : public IActivityHandler, public CJNIMainActivity, public CJNIBr

static bool StartActivity(const std::string &package, const std::string &intent = std::string(), const std::string &dataType = std::string(), const std::string &dataURI = std::string());
static std::vector <androidPackage> GetApplications();
static bool GetIconSize(const std::string &packageName, int *width, int *height);
static bool GetIcon(const std::string &packageName, void* buffer, unsigned int bufSize);

/*!
* \brief If external storage is available, it returns the path for the external storage (for the specified type)
Expand Down
2 changes: 1 addition & 1 deletion xbmc/android/jni/ApplicationInfo.cpp
Expand Up @@ -23,7 +23,7 @@

using namespace jni;

CJNIApplicationInfo::CJNIApplicationInfo(const jhobject &object) : CJNIBase(object)
CJNIApplicationInfo::CJNIApplicationInfo(const jhobject &object) : CJNIPackageItemInfo(object)
,sourceDir( jcast<std::string>(get_field<jhstring>(m_object, "sourceDir")))
,publicSourceDir( jcast<std::string>(get_field<jhstring>(m_object, "publicSourceDir")))
,dataDir( jcast<std::string>(get_field<jhstring>(m_object, "dataDir")))
Expand Down
3 changes: 2 additions & 1 deletion xbmc/android/jni/ApplicationInfo.h
Expand Up @@ -20,8 +20,9 @@
*/

#include "JNIBase.h"
#include "PackageItemInfo.h"

class CJNIApplicationInfo : public CJNIBase
class CJNIApplicationInfo : public CJNIPackageItemInfo
{
public:
CJNIApplicationInfo(const jni::jhobject &object);
Expand Down
2 changes: 2 additions & 0 deletions xbmc/android/jni/Context.cpp
Expand Up @@ -45,6 +45,7 @@
#include "Window.h"
#include "View.h"
#include "Build.h"
#include "DisplayMetrics.h"

#include <android/native_activity.h>

Expand Down Expand Up @@ -84,6 +85,7 @@ void CJNIContext::PopulateStaticFields()
CJNIMediaFormat::PopulateStaticFields();
CJNIView::PopulateStaticFields();
CJNIBuild::PopulateStaticFields();
CJNIDisplayMetrics::PopulateStaticFields();
}

CJNIPackageManager CJNIContext::GetPackageManager()
Expand Down
50 changes: 50 additions & 0 deletions xbmc/android/jni/DisplayMetrics.cpp
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include "DisplayMetrics.h"
#include "jutils/jutils-details.hpp"

using namespace jni;
const char *CJNIDisplayMetrics::m_classname = "android/util/DisplayMetrics";

int CJNIDisplayMetrics::DENSITY_DEFAULT(-1);
int CJNIDisplayMetrics::DENSITY_HIGH(-1);
int CJNIDisplayMetrics::DENSITY_LOW(-1);
int CJNIDisplayMetrics::DENSITY_MEDIUM(-1);
int CJNIDisplayMetrics::DENSITY_TV(-1);
int CJNIDisplayMetrics::DENSITY_XHIGH(-1);
int CJNIDisplayMetrics::DENSITY_XXHIGH(-1);
int CJNIDisplayMetrics::DENSITY_XXXHIGH(-1);

void CJNIDisplayMetrics::PopulateStaticFields()
{
jhclass clazz = find_class(m_classname);

DENSITY_DEFAULT = get_static_field<jint>(clazz, "DENSITY_DEFAULT");
DENSITY_HIGH = get_static_field<jint>(clazz, "DENSITY_HIGH");
DENSITY_LOW = get_static_field<jint>(clazz, "DENSITY_LOW");
DENSITY_MEDIUM = get_static_field<jint>(clazz, "DENSITY_MEDIUM");
DENSITY_TV = get_static_field<jint>(clazz, "DENSITY_TV");
DENSITY_XHIGH = get_static_field<jint>(clazz, "DENSITY_XHIGH");
if(CJNIBase::GetSDKVersion() >= 16)
DENSITY_XXHIGH = get_static_field<jint>(clazz, "DENSITY_XXHIGH");
if(CJNIBase::GetSDKVersion() >= 18)
DENSITY_XXXHIGH = get_static_field<jint>(clazz, "DENSITY_XXXHIGH");
}
42 changes: 42 additions & 0 deletions xbmc/android/jni/DisplayMetrics.h
@@ -0,0 +1,42 @@
#pragma once
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include "JNIBase.h"

class CJNIDisplayMetrics
{
public:
static int DENSITY_DEFAULT;
static int DENSITY_HIGH;
static int DENSITY_LOW;
static int DENSITY_MEDIUM;
static int DENSITY_TV;
static int DENSITY_XHIGH;
static int DENSITY_XXHIGH;
static int DENSITY_XXXHIGH;

static void PopulateStaticFields();

private:
CJNIDisplayMetrics();
~CJNIDisplayMetrics() {};
static const char *m_classname;
};
3 changes: 3 additions & 0 deletions xbmc/android/jni/Makefile.in
Expand Up @@ -54,6 +54,9 @@ SRCS += Activity.cpp
SRCS += SystemProperties.cpp
SRCS += Display.cpp
SRCS += WindowManager.cpp
SRCS += Resources.cpp
SRCS += PackageItemInfo.cpp
SRCS += DisplayMetrics.cpp

LIB = jni.a

Expand Down
30 changes: 30 additions & 0 deletions xbmc/android/jni/PackageItemInfo.cpp
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include "PackageItemInfo.h"
#include "jutils/jutils-details.hpp"

using namespace jni;
const char *CJNIPackageItemInfo::m_classname = "android/content/pm/PackageItemInfo";

CJNIPackageItemInfo::CJNIPackageItemInfo(const jhobject &object) : CJNIBase(object)
,icon( get_field<int>(m_object, "icon"))
{
}
36 changes: 36 additions & 0 deletions xbmc/android/jni/PackageItemInfo.h
@@ -0,0 +1,36 @@
#pragma once
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include "JNIBase.h"

class CJNIPackageItemInfo : public CJNIBase
{
public:
CJNIPackageItemInfo(const jni::jhobject &object);

int icon;

protected:
CJNIPackageItemInfo();
~CJNIPackageItemInfo() {};
static const char *m_classname;

};
14 changes: 14 additions & 0 deletions xbmc/android/jni/PackageManager.cpp
Expand Up @@ -24,6 +24,7 @@
#include "List.h"
#include "CharSequence.h"
#include "ApplicationInfo.h"
#include "Resources.h"
#include "jutils/jutils-details.hpp"

using namespace jni;
Expand Down Expand Up @@ -71,3 +72,16 @@ CJNIList<CJNIApplicationInfo> CJNIPackageManager::getInstalledApplications(int f
flags);
}

CJNIResources CJNIPackageManager::getResourcesForApplication(const std::string &package)
{
return call_method<jhobject>(m_object,
"getResourcesForApplication", "(Ljava/lang/String;)Landroid/content/res/Resources;",
jcast<jhstring>(package));
}

CJNIResources CJNIPackageManager::getResourcesForApplication(const CJNIApplicationInfo &info)
{
return call_method<jhobject>(m_object,
"getResourcesForApplication", "(Landroid/content/pm/ApplicationInfo;)Landroid/content/res/Resources;",
info.get_raw());
}
3 changes: 3 additions & 0 deletions xbmc/android/jni/PackageManager.h
Expand Up @@ -26,6 +26,7 @@ class CJNIIntent;
class CJNIDrawable;
class CJNIApplicationInfo;
class CJNICharSequence;
class CJNIResources;

class CJNIPackageManager : public CJNIBase
{
Expand All @@ -38,6 +39,8 @@ class CJNIPackageManager : public CJNIBase
CJNIDrawable getApplicationIcon(const std::string &package);
CJNIList<CJNIApplicationInfo> getInstalledApplications(int flags);
CJNICharSequence getApplicationLabel(const CJNIApplicationInfo &info);
CJNIResources getResourcesForApplication(const std::string &package);
CJNIResources getResourcesForApplication(const CJNIApplicationInfo &info);

static void PopulateStaticFields();
static int GET_ACTIVITIES;
Expand Down
33 changes: 33 additions & 0 deletions xbmc/android/jni/Resources.cpp
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include "Resources.h"
#include "Drawable.h"
#include "jutils/jutils-details.hpp"

using namespace jni;

CJNIDrawable CJNIResources::getDrawableForDensity(int id, int density)
{
return call_method<jhobject>(m_object,
"getDrawableForDensity", "(II)Landroid/graphics/drawable/Drawable;",
id, density);
}

0 comments on commit 6b91dde

Please sign in to comment.