/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
*
* 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.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Tizen.Applications
{
///
/// This class provides methods and properties to get information of the application.
///
/// 3
public class ApplicationRunningContext : IDisposable
{
private const string LogTag = "Tizen.Applications";
private bool _disposed = false;
internal IntPtr _contextHandle = IntPtr.Zero;
private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
internal ApplicationRunningContext(IntPtr contextHandle)
{
_contextHandle = contextHandle;
}
///
/// A constructor of ApplicationRunningContext that takes the application ID.
///
/// Application ID.
/// Thrown when failed because of an invalid argument.
/// Thrown when failed because of the "application not exist" error or the system error.
/// Thrown when failed because of out of memory.
/// 3
public ApplicationRunningContext(string applicationId)
{
IntPtr contextHandle = IntPtr.Zero;
err = Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out contextHandle);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
switch (err)
{
case Interop.ApplicationManager.ErrorCode.InvalidParameter:
throw new ArgumentException("Invalid Parameter.");
case Interop.ApplicationManager.ErrorCode.NoSuchApp:
throw new InvalidOperationException("No such application.");
case Interop.ApplicationManager.ErrorCode.OutOfMemory:
throw new OutOfMemoryException("Out of memory");
default:
throw new InvalidOperationException("Invalid Operation.");
}
}
_contextHandle = contextHandle;
}
///
/// A constructor of ApplicationRunningContext that takes the application id.
///
/// application id.
/// instance id.
/// Thrown when failed of invalid argument.
/// Thrown when failed because of application not exist error or system error.
/// Thrown when failed because of out of memory.
/// 4
[EditorBrowsable(EditorBrowsableState.Never)]
public ApplicationRunningContext(string applicationId, string instanceId)
{
IntPtr contextHandle = IntPtr.Zero;
err = Interop.ApplicationManager.AppManagerGetAppContextByInstanceId(applicationId, instanceId, out contextHandle);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
switch (err)
{
case Interop.ApplicationManager.ErrorCode.InvalidParameter:
throw new ArgumentException("Invalid Parameter.");
case Interop.ApplicationManager.ErrorCode.NoSuchApp:
throw new InvalidOperationException("No such application.");
case Interop.ApplicationManager.ErrorCode.OutOfMemory:
throw new OutOfMemoryException("Out of memory");
default:
throw new InvalidOperationException("Invalid Operation.");
}
}
_contextHandle = contextHandle;
}
///
/// Destructor of the class.
///
~ApplicationRunningContext()
{
Dispose(false);
}
///
/// Enumeration for the application state.
///
/// 3
public enum AppState
{
///
/// The undefined state.
///
Undefined = 0,
///
/// The UI application is running in the foreground.
///
Foreground,
///
/// The UI application is running in the background.
///
Background,
///
/// The service application is running.
///
Service,
///
/// The application is terminated.
///
Terminated,
}
///
/// Gets the application ID.
///
/// 3
public string ApplicationId
{
get
{
string appid = string.Empty;
err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the application id. err = " + err);
}
return appid;
}
}
///
/// Gets whether the application is terminated.
///
/// 6
public bool IsTerminated
{
get
{
bool isRunning = false;
string appid = string.Empty;
err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the application id. err = " + err);
}
else
{
Interop.ApplicationManager.AppManagerIsRunning(appid, out isRunning);
err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get is running. err = " + err);
}
}
return !isRunning;
}
}
///
/// Gets the package ID of the application.
///
/// 3
public string PackageId
{
get
{
string packageid = string.Empty;
err = Interop.ApplicationManager.AppContextGetPackageId(_contextHandle, out packageid);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the package id. err = " + err);
}
return packageid;
}
}
///
/// Gets the application's process ID.
///
/// 3
public int ProcessId
{
get
{
int pid = 0;
err = Interop.ApplicationManager.AppContextGetPid(_contextHandle, out pid);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the process id. err = " + err);
}
return pid;
}
}
///
/// Gets the state of the application.
///
///
/// Note that application's state might be changed after you get app_context. This API just returns the state of application when you get the app_context.
///
/// 3
public AppState State
{
get
{
int state = 0;
err = Interop.ApplicationManager.AppContextGetAppState(_contextHandle, out state);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the application state. err = " + err);
}
return (AppState)state;
}
}
///
/// Gets whether the application is sub application of the application group.
///
/// 3
public bool IsSubApp
{
get
{
bool subapp = false;
err = Interop.ApplicationManager.AppContextIsSubApp(_contextHandle, out subapp);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
Log.Warn(LogTag, "Failed to get the IsSubApp value. err = " + err);
}
return subapp;
}
}
///
/// Terminates the application.
///
/// Thrown when failed of invalid argument.
/// Thrown when failed because of permission denied.
/// Thrown when failed because of system error.
/// http://tizen.org/privilege/appmanager.kill
/// 4
[EditorBrowsable(EditorBrowsableState.Never)]
public void Terminate()
{
err = Interop.ApplicationManager.AppManagerTerminateApp(_contextHandle);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
switch (err)
{
case Interop.ApplicationManager.ErrorCode.InvalidParameter:
throw new ArgumentException("Invalid argument.");
case Interop.ApplicationManager.ErrorCode.PermissionDenied:
throw new UnauthorizedAccessException("Permission denied.");
default:
throw new InvalidOperationException("Invalid Operation.");
}
}
}
///
/// Terminates the application without restarting.
///
/// Thrown when failed of invalid argument.
/// Thrown when failed because of permission denied.
/// Thrown when failed because of system error.
/// http://tizen.org/privilege/appmanager.kill
/// 10
[EditorBrowsable(EditorBrowsableState.Never)]
public void TerminateWithoutRestarting()
{
err = Interop.ApplicationManager.AppManagerTerminateAppWithoutRestarting(_contextHandle);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
switch (err)
{
case Interop.ApplicationManager.ErrorCode.InvalidParameter:
throw new ArgumentException("Invalid argument.");
case Interop.ApplicationManager.ErrorCode.PermissionDenied:
throw new UnauthorizedAccessException("Permission denied.");
default:
throw new InvalidOperationException("Invalid Operation.");
}
}
}
///
/// Resumes the running application.
///
/// Thrown when failed of invalid argument.
/// Thrown when failed because of permission denied.
/// Thrown when failed because of system error.
/// http://tizen.org/privilege/appmanager.launch
/// 4
public void Resume()
{
err = Interop.ApplicationManager.AppManagerResumeApp(_contextHandle);
if (err != Interop.ApplicationManager.ErrorCode.None)
{
switch (err)
{
case Interop.ApplicationManager.ErrorCode.InvalidParameter:
throw new ArgumentException("Invalid argument.");
case Interop.ApplicationManager.ErrorCode.PermissionDenied:
throw new UnauthorizedAccessException("Permission denied.");
default:
throw new InvalidOperationException("Invalid Operation.");
}
}
}
///
/// Releases all resources used by the ApplicationRunningContext class.
///
/// 3
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (_disposed)
return;
if (_contextHandle != IntPtr.Zero)
{
Interop.ApplicationManager.AppContextDestroy(_contextHandle);
_contextHandle = IntPtr.Zero;
}
_disposed = true;
}
}
}